react native navigation: android app crash (java.lang.IllegalArgumentException: You need to use a Th

Posted

技术标签:

【中文标题】react native navigation: android app crash (java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme)【英文标题】:react native navigation: android app crashes (java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme) 【发布时间】:2018-03-20 13:06:48 【问题描述】:

我们最近开始使用 react-native-navigation (而不是 react-navigation )。 ios 版本运行良好,但我们无法配置 android 应用程序。我已经根据安装手册和其他示例修改了 MainActivity 和 MainApplication.java,但仍然无法正常工作。

主活动:

package com.client;
import com.reactnativenavigation.controllers.SplashActivity;

public class MainActivity extends SplashActivity 


MainApplication.java:

package com.client;

import com.oblador.vectoricons.VectorIconsPackage;
import com.amazonaws.RNAWSCognitoPackage;
import com.github.yamill.orientation.OrientationPackage;
import org.reactnative.camera.RNCameraPackage;
import com.inprogress.reactnativeyoutube.ReactNativeYouTube;
import io.sentry.RNSentryPackage;
import com.rnfs.RNFSPackage;
import com.transistorsoft.rnbackgroundgeolocation.RNBackgroundGeolocation;
import com.react.rnspinkit.RNSpinkitPackage;
import com.avishayil.rnrestart.ReactNativeRestartPackage;
import com.geektime.rnonesignalandroid.ReactNativeOneSignalPackage;
import com.imagepicker.ImagePickerPackage;
import com.AlexanderZaytsev.RNI18n.RNI18nPackage;
import com.facebook.reactnative.androidsdk.FBSDKPackage;
import com.learnium.RNDeviceInfo.RNDeviceInfo;
import io.branch.referral.Branch;
import com.vydia.RNUploader.UploaderReactPackage;
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;
import com.airbnb.android.react.maps.MapsPackage;

import java.util.Arrays;
import java.util.List;

import com.facebook.FacebookSdk;
import com.facebook.CallbackManager;
import com.reactnativenavigation.NavigationApplication;

import android.content.Intent;
import android.app.Activity;
import com.reactnativenavigation.controllers.ActivityCallbacks;
import io.branch.rnbranch.*;
import android.support.annotation.Nullable;

public class MainApplication extends NavigationApplication 

  private static CallbackManager mCallbackManager = CallbackManager.Factory.create();

  protected static CallbackManager getCallbackManager() 
    return mCallbackManager;
  

  private Activity activityInstance;

  @Override
  public void onCreate() 
    super.onCreate();
    setActivityCallbacks(new ActivityCallbacks() 
        @Override
        public void onActivityStarted(Activity activity) 
          activityInstance = activity;
          RNBranchModule.initSession(activity.getIntent().getData(), activity);
        
        @Override
        public void onNewIntent(Intent intent) 
          activityInstance.setIntent(intent);
        
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) 
            mCallbackManager.onActivityResult(requestCode, resultCode, data);
        
    );
    FacebookSdk.sdkInitialize(getApplicationContext());
    Branch.getAutoInstance(this);
    SoLoader.init(this, /* native exopackage */ false);
  

  @Override
   public boolean isDebug() 
       return BuildConfig.DEBUG;
   

  @Nullable
  @Override
  public List<ReactPackage> createAdditionalReactPackages() 
    return Arrays.<ReactPackage>asList(
    new VectorIconsPackage(),
    new RNAWSCognitoPackage(),
    new OrientationPackage(),
    new RNCameraPackage(),
    new ReactNativeYouTube(),
    new RNSentryPackage(MainApplication.this),
    new RNFSPackage(),
    new RNSpinkitPackage(),
    new ReactNativeRestartPackage(),
    new ReactNativeOneSignalPackage(),
    new ImagePickerPackage(),
    new RNI18nPackage(),
    new FBSDKPackage(mCallbackManager),
    new RNDeviceInfo(),
    new RNBranchPackage(),
    new UploaderReactPackage(),
    new RNBackgroundGeolocation(),
    new MapsPackage()
    );
  

  @Nullable
  @Override
  public String getJSMainModuleName() 
    return "index";
  

AndroidManifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.client">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <application
      android:largeHeap="true"
      android:name=".MainApplication"
      android:allowBackup="true"
      android:label="@string/app_name"
      android:icon="@mipmap/iamsmart">
      <!-- google maps api key -->
      <meta-data
      android:name="com.google.android.geo.API_KEY"
      android:value="xxxxx"/>
      <meta-data
      android:name="com.transistorsoft.locationmanager.license"
      android:value="xxxxx"/>
      <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize"
        android:launchMode="singleTask"
        android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <!-- Accepts URIs that begin with "http://link.xxxx.com/” -->
            <data android:scheme="http"
                android:host="link.xxxx.com"
                android:pathPrefix="/" />
            <!-- note that the leading "/" is required for pathPrefix-->
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <!-- Accepts URIs that begin with "xxxx://users” -->
            <data android:scheme="xxxx"
                android:host="users" />
        </intent-filter>
        <!-- Branch URI Scheme -->
        <intent-filter>
            <data android:scheme="invite.xxxx.com" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>

        <!-- Branch App Links (optional) -->
        <intent-filter android:autoVerify="true">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="https" android:host="invite.xxxx.com" />
        </intent-filter>
      </activity>
      <!-- Branch init -->
      <meta-data android:name="io.branch.sdk.BranchKey" android:value="xxxxx" />
      <meta-data android:name="io.branch.sdk.BranchKey.test" android:value="kxxxx" />
      <meta-data android:name="io.branch.sdk.TestMode" android:value="false" />

      <!-- Branch install referrer tracking (optional) -->
      <receiver android:name="io.branch.referral.InstallListener" android:exported="true">
          <intent-filter>
              <action android:name="com.android.vending.INSTALL_REFERRER" />
          </intent-filter>
      </receiver>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    </application>

</manifest>

日志猫:

03-20 12:57:55.314 20308 20308 D AndroidRuntime: Shutting down VM
03-20 12:57:55.318 20308 20308 E AndroidRuntime: FATAL EXCEPTION: main
03-20 12:57:55.318 20308 20308 E AndroidRuntime: Process: com.xxxxx.android.client, PID: 20308
03-20 12:57:55.318 20308 20308 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfocom.xxxxx.android.client/com.reactnativenavigation.controllers.NavigationActivity: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at android.app.ActivityThread.-wrap11(Unknown Source:0)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:106)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:164)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:6494)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: Caused by: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:33)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at android.support.design.widget.AppBarLayout.<init>(AppBarLayout.java:158)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at android.support.design.widget.AppBarLayout.<init>(AppBarLayout.java:151)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at com.reactnativenavigation.views.TopBar.<init>(TopBar.java:43)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at com.reactnativenavigation.screens.Screen.createTopBar(Screen.java:164)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at com.reactnativenavigation.screens.Screen.createAndAddTopBar(Screen.java:159)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at com.reactnativenavigation.screens.Screen.createViews(Screen.java:122)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at com.reactnativenavigation.screens.Screen.<init>(Screen.java:62)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at com.reactnativenavigation.screens.SingleScreen.<init>(SingleScreen.java:16)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at com.reactnativenavigation.screens.ScreenFactory.create(ScreenFactory.java:23)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at com.reactnativenavigation.screens.ScreenStack.pushInitialScreen(ScreenStack.java:96)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at com.reactnativenavigation.layouts.SingleScreenLayout.pushInitialScreen(SingleScreenLayout.java:92)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at com.reactnativenavigation.layouts.SingleScreenLayout.createStack(SingleScreenLayout.java:87)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at com.reactnativenavigation.layouts.SingleScreenLayout.createLayout(SingleScreenLayout.java:64)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at com.reactnativenavigation.layouts.SingleScreenLayout.<init>(SingleScreenLayout.java:56)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at com.reactnativenavigation.layouts.LayoutFactory.createSingleScreenLayout(LayoutFactory.java:20)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at com.reactnativenavigation.layouts.LayoutFactory.create(LayoutFactory.java:15)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at com.reactnativenavigation.controllers.NavigationActivity.createLayout(NavigationActivity.java:100)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at com.reactnativenavigation.controllers.NavigationActivity.onCreate(NavigationActivity.java:81)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at android.app.Activity.performCreate(Activity.java:7009)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at android.app.Activity.performCreate(Activity.java:7000)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
03-20 12:57:55.318 20308 20308 E AndroidRuntime: 	... 9 more
03-20 12:57:55.323 20308 20308 D io.sentry.android.event.helper.AndroidEventBuilderHelper: Proguard UUIDs file not found.
03-20 12:57:55.380 20308 20354 I react-native-sentry: captureEvent ' NativeMap: "level":"log","category":"console","message":"Running application \"Menu\" with appParams: \"initialProps\":\"screenInstanceID\":\"screenInstanceID4\",\"navigatorID\":\"navigatorID3_nav\",\"navigatorEventID\":\"screenInstanceID4_events\",\"rootTag\":1. __DEV__ === true, development-level warning are ON, performance optimizations are OFF","timestamp":1521550675.308 '
03-20 12:57:55.453  1582  2757 W ActivityManager:   Force finishing activity com.xxxxx.android.client/com.reactnativenavigation.controllers.NavigationActivity

【问题讨论】:

您找到解决问题的方法了吗?我得到了完全相同的错误。 【参考方案1】:

根据日志,这不是与分支相关的问题。

AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfocom.xxxxx.android.client/com.reactnativenavigation.controllers.NavigationActivity: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.

*** 上还有一些其他资源可以解决您的问题:

You need to use a Theme.AppCompat theme (or descendant) with this activity

【讨论】:

以上是关于react native navigation: android app crash (java.lang.IllegalArgumentException: You need to use a Th的主要内容,如果未能解决你的问题,请参考以下文章

使用 wix@react-native-navigation 运行 react-native 应用程序时出错

react-native + react-native-tab-navigator 实现 TabBar

在带有 wix/react-native-navigation 的模态中使用 react-native-gesture-handler (RNGH)

react-native填坑--react-navigation

React Navigation 与 React Native Navigation [关闭]

如何在 react-native 中结合使用 Drawer Navigation 和 Stack Navigator?