开始活动(意图);似乎总是使应用程序崩溃
Posted
技术标签:
【中文标题】开始活动(意图);似乎总是使应用程序崩溃【英文标题】:startActivity(intent); seems to always crash the app 【发布时间】:2020-04-03 02:25:59 【问题描述】:当应用程序打开时,它会成功打开并运行我们的“MenuActivity.java”。但是当我们单击一个按钮来启动一个单独的活动“MainActivity.java”时,应用程序只会崩溃,没有错误或跟踪堆栈或任何东西。 这是MenuActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Button button = findViewById(R.id.buttonDiceRoller);
button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Intent intent = new Intent(MenuActivity.this, MainActivity.class);
startActivity(intent);
finish();
);
这里是 MainActivity 的开始
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
这是我们的 android 清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cs125final">
<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=".MenuActivity"
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"
android:label="CS125Final" >
</activity>
</application>
</manifest>
这是应用程序运行时的样子。 If you want to view it not really necesarry
当您单击 diceRoller 按钮时,应用关闭,屏幕显示“您的应用一直在停止”。
为什么会这样? 海量logcat:
2019-12-09 17:37:53.628 12050-12050/com.example.cs125final E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cs125final, PID: 12050
java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.cs125final/com.example.cs125final.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:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
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 androidx.appcompat.app.AppCompatDelegateImpl.setSupportActionBar(AppCompatDelegateImpl.java:345)
at androidx.appcompat.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)
at com.example.cs125final.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2019-12-09 17:37:53.636 12050-12050/com.example.cs125final I/Process: Sending signal. PID: 12050 SIG: 9
【问题讨论】:
我想评论一下,到目前为止,堆栈上没有其他解决方案有效。 提供崩溃日志adb logcat
(或 Android Studio 中的 Logcat 选项卡)将有一个堆栈跟踪,可以为您指明正确的方向。没有这个,几乎是不可能的。
已添加,谢谢!
错误信息非常准确地告诉你问题: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.
【参考方案1】:
两个主题都需要设置为
android:theme="@style/AppTheme.NoActionBar"
【讨论】:
【参考方案2】:按照你的风格,这样定义父级
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
【讨论】:
以上是关于开始活动(意图);似乎总是使应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章