React Native Splash Screen:不幸的是,<app>在SplashScreen.show()之后停止了

时间:2018-10-11 11:01:47

标签: android reactjs react-native splash-screen oncreate

我正在构建一个React Native App,我在启动画面中使用了React Native Splash Screen

让我先给你我的代码,然后我会解释这个问题。

res/drawable/background_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/blue" />
    <item
        android:width="200dp"
        android:height="200dp"
        android:drawable="@mipmap/icon_splash"
        android:gravity="center" />
</layer-list>

SplashActivity.java

package com.painbutton;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

MainActivity.java

package com.painbutton;

import com.facebook.react.ReactActivity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;

import org.devio.rn.splashscreen.SplashScreen;

public class MainActivity extends ReactActivity {
    /**
     * Returns the name of the main component registered from JavaScript. This is
     * used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "painbutton";
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Intent intent = new Intent("onConfigurationChanged");
        intent.putExtra("newConfig", newConfig);
        this.sendBroadcast(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SplashScreen.show(this);
        super.onCreate(savedInstanceState);
    }
}

AndroidManifest.xml

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

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

    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:allowBackup="false"
      android:theme="@style/AppTheme">
        <activity
            android:name=".SplashActivity"
            android:theme="@style/SplashTheme"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize"
          android:exported="true">
      </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    </application>

</manifest>

我的图像在res/内部,然后每个像素密度都有一个icon.png。

当我知道运行该应用程序时,它崩溃,提示“不幸的是,painbutton已停止”。

这是错误日志:

10-11 12:51:10.441 537-537/com.painbutton E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.painbutton, PID: 537
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.painbutton/com.painbutton.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f09001d type #0x1 is not valid
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f09001d type #0x1 is not valid
        at android.content.res.Resources.loadXmlResourceParser(Resources.java:2779)
        at android.content.res.Resources.getLayout(Resources.java:1165)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
        at android.app.Dialog.setContentView(Dialog.java:512)
        at org.devio.rn.splashscreen.SplashScreen$1.run(SplashScreen.java:32)
        at android.app.Activity.runOnUiThread(Activity.java:5511)
        at org.devio.rn.splashscreen.SplashScreen.show(SplashScreen.java:27)
        at org.devio.rn.splashscreen.SplashScreen.show(SplashScreen.java:49)
        at org.devio.rn.splashscreen.SplashScreen.show(SplashScreen.java:56)
        at com.painbutton.MainActivity.onCreate(MainActivity.java:30)
        at android.app.Activity.performCreate(Activity.java:6237)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

这是怎么回事?为什么我的应用程序崩溃了?

2 个答案:

答案 0 :(得分:0)

我的第一个答案被删除了。我附加了信息,以便希望它不会被删除,因为我已对其进行了修复。我关注了this tutorial。确保观看到最后。如果跳过最后2分钟,您仍然会遇到我在此问题中描述的相同问题。

对我来说,解决方法是在res/layouts/launch_screen.xml中创建文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue"
    android:gravity="center">

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_marginTop="24dp"
        android:src="@drawable/launch_screen"/>

</LinearLayout>

并将<color name="primary_dark">#329baf</color>添加到我的res/values/colors.xml文件中。

答案 1 :(得分:0)

我终于能够解决这个问题。确保您执行以下操作。

  1. 创建一个名为res/layouts/launch_screen.xml的文件。您还可以通过UI进行查看和编辑。 Click here来查看用户界面的外观。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical"
>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="center"
        android:src="@drawable/splash" />
</LinearLayout>
  1. 将以下代码段添加到values/styles.xml。这将允许您在启动时从清单文件中调用此Splash主题。
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@layout/launch_screen</item>
</style>
  1. 确保您的AndroidManifest.xml具有以下内容:
.
.
<activity
    android:name=".MainActivity"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@style/SplashTheme" <------ NOTE THIS
.
.

  1. 最后,请记住按照软件包文档中的说明在您的SplashScreen.show(this);中添加MainActivity.java

此整个练习将确保在应用程序启动期间以及在加载React Native捆绑包期间均显示启动画面。祝好运!