活动更改后应用程序崩溃

时间:2018-01-30 12:35:29

标签: java android android-studio android-activity android-manifest

我已经实现了一个启动画面,在3秒后完成并加载Main活动,但是,它一直崩溃并且说应用程序已停止。

虽然我成功地修改了清单,但是,我确信问题源于清单本身。

对此问题的任何帮助将不胜感激。

在Android上很新,如果缺少任何信息,请道歉。

抱歉凌乱的LogCat!

SplashScreen.java

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

    public class SplashScreen extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_splash_screen);

            Thread myThread = new Thread() {

                @Override
                public void run() {

                    try {
                        sleep (3000);
                        Intent intent = new Intent (getApplicationContext(),MainActivity.class);
                        startActivity(intent);
                        finish();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };

            myThread.start();
        }
}

清单

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

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity
                android:name=".SplashScreen"
                android:label="@string/app_name"
                android:theme="@style/AppTheme.NoActionBar">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
            <activity android:name=".MainActivity"/>
        </application>
    </manifest>

logcat的

    01-30 12:31:37.824 4954-4954/? I/zygote: Not late-enabling -Xcheck:jni (already on)
01-30 12:31:38.109 4954-4954/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
01-30 12:31:38.529 4954-4954/com.example.benf.myapplication I/InstantRun: starting instant run server: is main process
01-30 12:31:38.939 4954-4981/com.example.benf.myapplication D/OpenGLRenderer: HWUI GL Pipeline
01-30 12:31:39.040 4954-4981/com.example.bef.myapplication I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
01-30 12:31:39.040 4954-4981/com.example.benf.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
01-30 12:31:39.040 4954-4981/com.example.benf.myapplication D/OpenGLRenderer: Swap behavior 1
01-30 12:31:39.041 4954-4981/com.example.benf.myapplication W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
01-30 12:31:39.041 4954-4981/com.example.benf.myapplication D/OpenGLRenderer: Swap behavior 0
01-30 12:31:39.046 4954-4981/com.example.benf.myapplication D/EGL_emulation: eglCreateContext: 0xb060ea60: maj 2 min 0 rcv 2
01-30 12:31:39.049 4954-4981/com.example.benf.myapplication D/EGL_emulation: eglMakeCurrent: 0xb060ea60: ver 2 0 (tinfo 0xa8849650)
01-30 12:31:39.177 4954-4981/com.example.benf.myapplication D/EGL_emulation: eglMakeCurrent: 0xb060ea60: ver 2 0 (tinfo 0xa8849650)
01-30 12:31:42.189 4954-4954/com.example.benf.myapplication D/AndroidRuntime: Shutting down VM
01-30 12:31:42.192 4954-4954/com.example.benf.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.example.benf.myapplication, PID: 4954
                                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.benf.myapplication/com.example.benf.myapplication.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                                                         at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                         at android.os.Looper.loop(Looper.java:164)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                                                      Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                                                                                         at android.support.v7.app.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:201)
                                                                                         at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:129)
                                                                                         at com.example.benf.myapplication.MainActivity.onCreate(MainActivity.java:24)
                                                                                         at android.app.Activity.performCreate(Activity.java:6999)
                                                                                         at android.app.Activity.performCreate(Activity.java:6990)
                                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
                                                                                         at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                                         at android.os.Looper.loop(Looper.java:164) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6494) 
                                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                                         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

3 个答案:

答案 0 :(得分:2)

您尚未为MainActivity指定任何主题。你在MainActivity中使用工具栏我想你的应用程序主题是android:theme="@style/AppTheme",它也提供了操作栏。所以这种情况会产生冲突。

在您的活动声明中更改为

<activity android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar"/>

答案 1 :(得分:2)

MainActivity课程中,您尝试添加工具栏,这就产生了问题。因为这个<activity android:name=".MainActivity"/> 应用程序主题将应用于您在Application标记android:theme="@style/AppTheme"

中指定的此活动

此主题本身也包含ActionBar。

将主题应用于您的MainActivity,就像您为SplashScreen活动所做的那样,

<activity android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar"/>

答案 2 :(得分:0)

在清单中为MainActivity指定: -

<activity android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar"/>