Android Studio:启动屏幕后应用程序崩溃[重复]
Posted
技术标签:
【中文标题】Android Studio:启动屏幕后应用程序崩溃[重复]【英文标题】:Android Studio: App crashing after Splash screen [duplicate] 【发布时间】:2019-08-16 03:02:41 【问题描述】:在我尝试实现启动画面后,我的应用程序崩溃了。 不知道问题出在哪里。
我尝试了 2 个小时来解决这个问题,甚至研究了网络,但不知何故我无法让它工作。 也许你们可以查看代码并给我一些提示?我会很感激的。
androidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
package="com.example.noctis">
<application tools:ignore="GoogleAppIndexingWarning"
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=".impressum"
android:label="@string/title_activity_impressum"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.Launcher">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
launch_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
<item android:drawable="@color/colorContentBackground"/>
<item>
<bitmap
android:src="@drawable/rsz_l52878klein"
android:gravity="center"/>
</item>
</layer-list>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:background="@color/colorContentBackground"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_
android:layout_
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
android:background="@color/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
android:layout_gravity="center_horizontal">
<ImageView
android:layout_
android:layout_
android:layout_gravity="center"
android:src="@drawable/rsz_l52878"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_
android:layout_
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
app:backgroundTint="@color/actionButton"
app:fabSize="normal"
android:src="@drawable/ic_add_black_32dp" />
</android.support.design.widget.CoordinatorLayout>
styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.Launcher" parent="AppTheme">
<item name="android:windowBackground">@drawable/launch_screen</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>
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
);
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
所以..会显示实际的初始屏幕,但之后应用程序就会崩溃。我不明白为什么:/
编辑:(崩溃日志)
2019-03-25 21:06:18.588 14651-14651/com.example.noctis E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.noctis, PID: 14651
java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.noctis/com.example.noctis.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:3107)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3250)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1947)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7032)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
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.AppCompatDelegateImpl.setSupportActionBar(AppCompatDelegateImpl.java:345)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)
at com.example.noctis.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:7327)
at android.app.Activity.performCreate(Activity.java:7318)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3087)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3250)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1947)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7032)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
2019-03-25 21:06:18.599 14651-14651/com.example.noctis I/Process: Sending signal. PID: 14651 SIG: 9
【问题讨论】:
你能发布你的logcat吗? 分享崩溃日志? 如果不查看您的日志 (logcat),我们将很难猜出您的问题所在。获取方法如下:developer.android.com/studio/debug/am-logcat 对不起,补充。 :) 好的,当我不设置操作栏时不会发生错误,但我需要设置它。 将应用的主题更改为:style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar" 【参考方案1】:是因为下面的错误:
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.
这意味着您的活动中有一个工具栏,但您的活动样式中也使用了一个工具栏。您需要在 MainActivity 中使用 "AppTheme.NoActionBar"
,如下所示:
<activity
android:name=".MainActivity"
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>
【讨论】:
【参考方案2】:您可能需要更新 MainActivity.java 的主题
<!-- 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>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
【讨论】:
【参考方案3】:这个 Activity 已经有一个由窗口装饰提供的操作栏。不要在主题中请求
Window.FEATURE_SUPPORT_ACTION_BAR
并将windowActionBar
设置为false
以使用工具栏。
完全按照它的建议去做怎么样?
<style name="AppTheme.Launcher" parent="AppTheme">
<item name="android:windowBackground">@drawable/launch_screen</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
【讨论】:
很遗憾没有工作 @Kevka "not working" 不是错误描述。为什么你甚至将启动器主题用于MainActivity
... 这已经显得很乱了。在任何情况下,尝试将两个 Toolbar
添加为 ActionBar
都行不通。
AppCompat does not support the current theme features: windowActionBar: false, windowActionBarOverlay: false, android:windowIsFloating: false, windowActionModeOverlay: false, windowNoTitle: false
以上是关于Android Studio:启动屏幕后应用程序崩溃[重复]的主要内容,如果未能解决你的问题,请参考以下文章
安装应用程序后在 android studio 中启动活动时出错
android studio项目启动画面无法正常关闭,应用程序运行完美但退出应用程序启动画面显示后,为啥[关闭]
升级到 Android Studio 2.0 后,我无法在 Android 模拟器中更改屏幕方向