Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to create the most simple native Android component, failing silently #13553

Closed
Carpetfizz opened this issue Apr 18, 2017 · 5 comments
Closed
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@Carpetfizz
Copy link

Carpetfizz commented Apr 18, 2017

Hello,

I'm trying to create a native UI component in Android. I believe I've followed the docs exactly, however the docs do not have an example of the ReactImageView which is the class that the view manager serves. I'm simply trying to display an Android TextView in RN. Once I get that to work I will move on to implementing my own class (so I don't strictly need TextView to work. The result of running this project is that literally nothing happens. It fails silently, and I don't see my "hello from android !" text appear on the screen and I don't know how to check if RN is even calling my Java code.

RCTScannerViewManager.java

public class RCTScannerViewManager extends SimpleViewManager<TextView> {

    public static final String REACT_CLASS = "RCTScannerView";

    @Override
    public String getName() {
        return REACT_CLASS;
    }

    @Override
    protected TextView createViewInstance(ThemedReactContext reactContext) {
        TextView tv = new TextView(reactContext);
        tv.setText("hello from android !");
        return tv;
    }
}

RCTScannerViewPackage.java

public class RCTScannerViewPackage implements ReactPackage {

    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }

    @Override
    public List<Class<? extends JavaScriptModule>> createJSModules() {
        return Collections.emptyList();
    }

    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Arrays.<ViewManager> asList(
                new RCTScannerViewManager()
        );
    }
}

MainApplication.java

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    public boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
              new RCTScannerViewPackage()
      );
    }
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
  }
}

ScannerView.js

import React, { Component, PropTypes } from 'react'
import { Text, View, requireNativeComponent } from 'react-native'

var iface = {
    name: 'RCTScannerView',
    propTypes: {
    	...View.propTypes
    }
}

module.exports = requireNativeComponent('RCTScannerView', iface);

index.android.js

import React, { Component } from 'react'
import { AppRegistry, Text, View } from 'react-native';
import ScannerView from './ScannerView';

class AwesomeProject extends Component {
  render() {

    return (
    	<View>
			<Text> Hello world @@ </Text>
			<ScannerView test="hello world"></ScannerView>
		</View>
    );
  }
}

AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject)

I would love to use RN in my project, but there seems to be a lack of documentation online about how to create native modules in Android. I have posted this issue on Stack Overflow, but no one has responded.

@Carpetfizz Carpetfizz changed the title Trying to create the most simple native Android component, getting errors Trying to create the most simple native Android component, failing silently Apr 18, 2017
@vonovak
Copy link
Contributor

vonovak commented Apr 18, 2017

where is your stackoverflow question?

@Carpetfizz
Copy link
Author

@rzsombor
Copy link

Answered on the SO thread, I think you forgot to add layouting attributes. Just managed to fall into the same pit :)

@Carpetfizz
Copy link
Author

That did the trick, thank you so much! Facebook should definitely provide an example like this in the docs!

@vonovak
Copy link
Contributor

vonovak commented May 16, 2017

@Carpetfizz you can improve the docs by yourself now that you've gained the knowledge!

@facebook facebook locked as resolved and limited conversation to collaborators May 24, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

4 participants