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"
再次进行编译,终于编译通过了,启动也不会报错了。