【错误记录】Android 应用运行报错 ( You need to use a Theme.AppCompat theme (or descendant) with this activity. )
生活随笔
收集整理的這篇文章主要介紹了
【错误记录】Android 应用运行报错 ( You need to use a Theme.AppCompat theme (or descendant) with this activity. )
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 一、報錯信息
- 二、解決方案
一、報錯信息
運行 Android 應(yīng)用時 , 報錯 ;
報錯信息如下 :
2021-08-04 21:04:12.067 26338-26338/com.example.plugin_hook E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.example.plugin_hook, PID: 26338java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.plugin_hook/com.example.plugin_hook.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2951)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086)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:1816)at android.os.Handler.dispatchMessage(Handler.java:106)at android.os.Looper.loop(Looper.java:193)at android.app.ActivityThread.main(ActivityThread.java:6718)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:858)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:846)at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:809)at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)at com.example.plugin_hook.MainActivity.onCreate(MainActivity.java:24)at android.app.Activity.performCreate(Activity.java:7144)at android.app.Activity.performCreate(Activity.java:7135)at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2931)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086) 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:1816) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6718) 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:858)二、解決方案
錯誤原因 : 使用了自定義 Application 之后 , 出現(xiàn)上述問題 ;
解決方案一 : 按照錯誤提示進行修改 ;
錯誤信息中 , 給出了解決方案提示 , You need to use a Theme.AppCompat theme (or descendant) with this activity.
使用 Theme.AppCompat 主題即可 ;
定義主題 :
<!-- Base application theme. --><style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. --><item name="colorPrimary">@color/purple_500</item><item name="colorPrimaryDark">@color/purple_700</item><item name="colorAccent">@color/teal_200</item></style>清單文件中使用主題 :
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.plugin"><applicationandroid: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=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>( 該方法可能無法生效 )
解決方案二 : 令 MainActivity 繼承自 android.app.Activity ;
public class MainActivity extends Activity {總結(jié)
以上是生活随笔為你收集整理的【错误记录】Android 应用运行报错 ( You need to use a Theme.AppCompat theme (or descendant) with this activity. )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Google Play】管理目标受众群
- 下一篇: 【错误记录】Google Play 上架