在现有Android应用中包含React-Nativ视图

时间:2016-11-22 16:10:18

标签: android react-native

我有一个非常简单的Android-App,其中一个视图有一个按钮到另一个视图。 在那个其他视图中,我必须显示一个React-Native组件。我按照https://facebook.github.io/react-native/docs/integration-with-existing-apps.html#integration-with-existing-apps上的说明操作,我的活动是我的小代码:

public class MyReactActivity extends Activity {

private ReactRootView mReactRootView;
private ReactInstanceManager mReactInstanceManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mReactRootView = new ReactRootView(this);

    mReactInstanceManager = ReactInstanceManager.builder()
            .setApplication(getApplication())
            .setBundleAssetName("main.jsbundle")
            .setJSMainModuleName("index.android.js")
            .addPackage(new MainReactPackage())
            .setUseDeveloperSupport(BuildConfig.DEBUG)
            .setInitialLifecycleState(LifecycleState.RESUMED)
            .build();
    mReactRootView.startReactApplication(mReactInstanceManager, "MyReactApp", null);

    setContentView(mReactRootView);
}

此应用程序启动正常,但当我触摸按钮更改视图时,应用程序崩溃:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.kris.testa, PID: 2612
              android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@65e68b3 -- permission denied for window type 2003
                  at android.view.ViewRootImpl.setView(ViewRootImpl.java:703)
                  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
                  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
                  at android.app.Dialog.show(Dialog.java:322)
                  at com.facebook.react.devsupport.DevSupportManagerImpl.handleReloadJS(DevSupportManagerImpl.java:538)
                  at com.facebook.react.ReactInstanceManagerImpl$3$1.run(ReactInstanceManagerImpl.java:386)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我做错了什么?有人可以帮忙吗?

更新: 我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kris.testa">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MyReactActivity" android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">

    </activity>
    <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />

</application>
</manifest> 

1 个答案:

答案 0 :(得分:1)

  

尝试在AndroidManifest中使用此权限。

     

android.permission.SYSTEM_ALERT_WINDOW

     

<强> on API >= 23 see

链接:https://stackoverflow.com/a/7570071/1796309

如果您使用的是Android版&gt; = 6.0,那么您必须遵循Android Permission模型,即您必须在运行时请求此权限。

如果您不想在运行时请求权限,只需将targetSdkVersion设置为22中的build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "ru.goenglish.goenglish"
        minSdkVersion 14
        targetSdkVersion 22          <<<<<<<<<<<<<<<<<<<<<<<<<
        versionCode 1
        versionName "1.0"
    }
...
}