【错误记录】Android 应用运行报错 ( You need to use a Theme.AppCompat theme (or descendant) with this activity. )
2023-06-13 09:17:54 时间
文章目录
一、报错信息
运行 Android 应用时 , 报错 ;
报错信息如下 :
2021-08-04 21:04:12.067 26338-26338/com.example.plugin_hook E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.plugin_hook, PID: 26338
java.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 之后 , 出现上述问题 ;
解决方案一 : 按照错误提示进行修改 ;
错误信息中 , 给出了解决方案提示 , 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">
<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=".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 {
相关文章
- android okio使用方法,Android 开源框架 Okio 原理剖析「建议收藏」
- Android源码学习之六——ActivityManager框架解析
- mac 电脑android环境变量设置,mac上Android环境变量配置[通俗易懂]
- robotium android,Robotium 测试Android apk安装包
- Android音视频——OMX 中 Nodeinstance 列表的管理与节点的操作
- Android scheme呼起App
- 【错误记录】Flutter / Android 报错 ( AAPT: error: attribute android:requestLegacyExternalStorage not found )
- 【错误记录】Android 中调用 Process 命令行执行指令 ( java.lang.IllegalThreadStateException: process hasn‘t exited )
- 【错误记录】Android 低版本使用分区存储错误 ( 低版本存储不得使用 MediaStore )
- 【错误记录】Android 编译时技术版本警告 ( 注解处理器与主应用支持的 Java 版本不匹配 )
- 【错误记录】Android Studio 编译时 Kotlin 代码编译报错 ( 升级支持库时处理 @NonNull 参数 )
- 【错误记录】Android Studio 编译报错 ( A problem occurred starting process ‘command ‘ninja.exe‘ ‘ )
- 【错误记录】Android Studio 向 GitHub 提交代码报错 ( Push failed: Failed with error: Could not read from remote )
- 【错误记录】Android NDK 编译报错 ( no known conversion from ‘unsigned char *‘ to ‘const char *‘ )
- 【错误记录】未安装该应用 ( 在 Android 12 之后 组件设置 android:exported=“false“ 属性 )
- 【错误记录】Android Studio 编译时 lint 检查报错 ( Error: Google Play requires that apps target API level 29 or )
- 基于Android设计模式之--SDK源码之策略模式的详解
- Causedby:android.os.NetworkOnMainThreadException错误解决办法
- Android实现弹出登陆框的方案