gpt4 book ai didi

java - 启动 Activity ( Intent );应用程序似乎总是崩溃

转载 作者:行者123 更新时间:2023-12-01 19:32:17 25 4
gpt4 key购买 nike

当应用程序打开时,它会成功打开并运行我们的“MenuActivity.java”。但是,当我们单击按钮启动单独的 Activity “MainActivity.java”时,应用程序只是崩溃,没有错误或跟踪堆栈或任何其他内容。这是菜单 Activity


@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 list 文件

<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 ComponentInfo{com.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

最佳答案

两个主题都需要设置为

android:theme="@style/AppTheme.NoActionBar"

关于java - 启动 Activity ( Intent );应用程序似乎总是崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59258108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com