Android 12 源码编译Apk 新建工程启动报NullPointerException

 Activity代码如下,就是一个纯净的新建MainActivity,继承自Activity。

布局文件如下

Android.mk编写如下,

 然而启动后会报如下NullPointerException,就很奇怪,查了很久,原来是因为在Android12的版本比较高了,出了兼容问题。

11-04 03:45:57.786  4985  4985 E test    : onCreate:App 
11-04 03:45:57.805  4985  4985 E AndroidRuntime: FATAL EXCEPTION: main
11-04 03:45:57.805  4985  4985 E AndroidRuntime: Process: com.example.myapplication, PID: 4985
11-04 03:45:57.805  4985  4985 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3707)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3864)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2253)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:106)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at android.os.Looper.loopOnce(Looper.java:201)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:288)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:7870)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at com.android.internal.app.WindowDecorActionBar.getDecorToolbar(WindowDecorActionBar.java:247)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at com.android.internal.app.WindowDecorActionBar.init(WindowDecorActionBar.java:199)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at com.android.internal.app.WindowDecorActionBar.<init>(WindowDecorActionBar.java:173)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at android.app.Activity.initWindowDecorActionBar(Activity.java:3509)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at android.app.Activity.setContentView(Activity.java:3527)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at com.example.myapplication.MainActivity.onCreate(MainActivity.java:11)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at android.app.Activity.performCreate(Activity.java:8059)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at android.app.Activity.performCreate(Activity.java:8039)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1341)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3688)
11-04 03:45:57.805  4985  4985 E AndroidRuntime: 	... 12 more

检查了半天,没有发现哪里有错啊,这就搞不懂了,于是就尝试着就尝试着继承一下比较新的AppCompatActivity

 再依赖一下所在的包androidx.appcompat_appcompat

 进行编译却报了如下错,java.lang.IllegalStateException: You need to use a Theme.AppCompat theme

11-04 03:46:07.195  5129  5129 E test    : onCreate:App 
11-04 03:46:07.228  5129  5129 E ThemeUtils: View class androidx.appcompat.widget.AppCompatImageView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).
11-04 03:46:07.233  5129  5129 E AndroidRuntime: FATAL EXCEPTION: main
11-04 03:46:07.233  5129  5129 E AndroidRuntime: Process: com.example.myapplication, PID: 5129
11-04 03:46:07.233  5129  5129 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3707)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3864)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2253)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:106)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at android.os.Looper.loopOnce(Looper.java:201)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:288)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:7870)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:845)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:808)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:695)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at com.example.myapplication.MainActivity.onCreate(MainActivity.java:39)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at android.app.Activity.performCreate(Activity.java:8059)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at android.app.Activity.performCreate(Activity.java:8039)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1341)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3688)
11-04 03:46:07.233  5129  5129 E AndroidRuntime: 	... 12 more

在AndroidManifest.xml中添加一个Theme.AppCompat类型的theme,如下是一个白天黑夜模式的主题。

android:theme="@style/Theme.AppCompat.DayNight"

 再次进行编译,终于编译通过了,启动也不会报错了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值