尽管尝试了所有建议,应用程序在启动画面后崩溃

时间:2016-03-06 16:26:56

标签: android exception android-activity crash

我是Android编程的新手。我正在尝试使用启动画面开发应用程序。看了几个教程&开始这样做了。该应用程序显示启动画面,但几秒钟后崩溃。尝试了StackExchage上给出的建议,但没有奏效。在将此帖子关闭为重复之前,请查看我的代码。

这是我的 SplashScreen.java

package collegeproject.mvjce.edu.boat;

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

/**
 * Created by AURO on 3/6/2016.
 */
public class SplashScreen extends AppCompatActivity {

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

        Thread myThread = new Thread() {
            @Override
            public void run() {
                try {
                    sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finally {
                    Intent startMain = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(startMain);
                    finish();
                }


            }
        };
        myThread.start();
    }
}

这是我的 splash_screen_layout.xml

<?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">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/splash_screen"
        android:id="@+id/splashView" />
</LinearLayout>

这是我的 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="collegeproject.mvjce.edu.boat">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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

03-06 21:38:22.446 18366-18366/collegeproject.mvjce.edu.boat E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: collegeproject.mvjce.edu.boat, PID: 18366
                                                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{collegeproject.mvjce.edu.boat/collegeproject.mvjce.edu.boat.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:2358)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
                                                                                   at android.app.ActivityThread.access$900(ActivityThread.java:154)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:135)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5292)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
                                                                                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.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198)
                                                                                   at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
                                                                                   at collegeproject.mvjce.edu.boat.MainActivity.onCreate(MainActivity.java:19)
                                                                                   at android.app.Activity.performCreate(Activity.java:5990)
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
                                                                                   at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:135) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5292) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 

这是我的 style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

这是我的 style.xml(v21)

<resources>>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
</resources>

我没有碰过MainActivity.java,activity_main.xml&amp; content.xml,因此我没有在这里给出他们的代码。如果你需要看一下,请告诉我。

我不知道问题似乎是什么。我已经看过10多个寻找修复的教程,已经在StackExchange和amp;上读过几个线程。相应地更改了我的代码,但似乎没有任何效果。因此我发布了这个问题。

感谢您的时间!

1 个答案:

答案 0 :(得分:1)

这是由于样式主题类型冲突。

只需对styles.xml

进行这些更改即可
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

并确保您的AndroidManifest.xml应用标记的主题属性设置为@style/AppTheme.NoActionBar

如果您将样式主题设置为类型DarkActionBarLightActionBar或类似ActionBar的内容,则无法使用自定义工具栏自定义ActionBar。

这就是为什么您要将主题设置为NoActionBar以使用setSuportActionBar(custom_toolbar)等功能进行自定义更改。