Android Studio按钮跳转新Activity崩溃问题求助
按钮跳转Activity崩溃问题解决
作为Android Studio新手,参考网上代码实现按钮跳转自定义的第二个Activity,但点击模拟器中的按钮时应用崩溃,期望按钮能打开创建的MainActivity2,相关代码及崩溃日志如下:
MainActivity代码(Kotlin)
package com.example.plaques import android.content.Intent import android.os.Bundle import android.view.View import androidx.activity.ComponentActivity class MainActivity : ComponentActivity() { // 注意:Kotlin不需要@Override注解,此注解会导致编译错误,需删除 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.application1) } fun ouvrir(view: View) { val intent = Intent(this, MainActivity2::class.java ) startActivity(intent) } }
布局文件(application1.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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_width="match_parent" android:layout_height="match_parent"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="open next activity" android:onClick="ouvrir" tools:ignore="MissingConstraints,UsingOnClickInXml" /> </androidx.constraintlayout.widget.ConstraintLayout> </LinearLayout>
崩溃日志
--------- beginning of crash 2022-12-01 14:31:10.752 2297-2297/com.example.plaques E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.plaques, PID: 2297 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.plaques/com.example.plaques.MainActivity2}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3676) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3813) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101) 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:2308) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7898) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:852) at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:815) at androidx.appcompat.app.AppCompatDelegateImpl.initWindowDecorActionBar(AppCompatDelegateImpl.java:556) at androidx.appcompat.app.AppCompatDelegateImpl.getSupportActionBar(AppCompatDelegateImpl.java:543) at androidx.appcompat.app.AppCompatDelegateImpl.invalidateOptionsMenu(AppCompatDelegateImpl.java:1226) at androidx.appcompat.app.AppCompatActivity.invalidateOptionsMenu(AppCompatActivity.java:314) at androidx.activity.ComponentActivity.invalidateMenu(ComponentActivity.java:553) at androidx.activity.ComponentActivity$$ExternalSyntheticLambda0.run(Unknown Source:2) at androidx.core.view.MenuHostHelper.addMenuProvider(MenuHostHelper.java:116) at androidx.activity.ComponentActivity.addMenuProvider(ComponentActivity.java:531) at androidx.fragment.app.FragmentActivity$HostCallbacks.addMenuProvider(FragmentActivity.java:736) at androidx.fragment.app.FragmentManager.attachController(FragmentManager.java:2793) at androidx.fragment.app.FragmentController.attachHost(FragmentController.java:117) at androidx.fragment.app.FragmentActivity.lambda$init$3$androidx-fragment-app-FragmentActivity(FragmentActivity.java:140) at androidx.fragment.app.FragmentActivity$$ExternalSyntheticLambda3.onContextAvailable(Unknown Source:2) at androidx.activity.contextaware.ContextAwareHelper.dispatchOnContextAvailable(ContextAwareHelper.java:99) at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:352) at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:217) at com.example.plaques.MainActivity2.onCreate(MainActivity2.kt:8) at android.app.Activity.performCreate(Activity.java:8290) at android.app.Activity.performCreate(Activity.java:8269) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1384) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3657) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3813) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101) 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:2308) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7898) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) 2022-12-01 14:31:10.868 2297-2297/com.example.plaques I/Process: Sending signal. PID: 2297 SIG: 9
问题原因及解决方法
崩溃日志核心错误:You need to use a Theme.AppCompat theme (or descendant) with this activity,说明MainActivity2使用的Activity类与应用主题不匹配,解决步骤如下:
1. 检查MainActivity2的继承类
打开MainActivity2.kt,查看其继承关系:
- 如果继承的是
AppCompatActivity,必须使用AppCompat或MaterialComponents主题; - 如果继承的是
ComponentActivity,可使用Material主题或AppCompat主题。
2. 修改主题配置
方法一:为MainActivity2单独设置主题
打开AndroidManifest.xml,找到MainActivity2的<activity>标签,添加android:theme属性:
<activity android:name=".MainActivity2" android:exported="false" android:theme="@style/Theme.MaterialComponents.DayNight.NoActionBar" />
方法二:修改全局主题
打开res/values/styles.xml(或res/values/themes.xml),将应用主题的父类改为AppCompat或MaterialComponents主题,例如:
<style name="Theme.Plaques" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <!-- 自定义主题属性 --> <item name="colorPrimary">@color/purple_500</item> <item name="colorPrimaryVariant">@color/purple_700</item> <item name="colorOnPrimary">@color/white</item> </style>
3. 修正MainActivity的语法错误
原代码中的@Override是Java语法,Kotlin无需添加,需删除该注解,否则会导致编译错误。
内容的提问来源于stack exchange,提问作者Rems
相关产品推荐
相关产品推荐

