升级到androidx之后发生的错误
错误代码
Process: com.smartonet.project, PID: 29623
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smartonet.project/com.smartonet.project.ui.main.activity.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2983)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3061)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1747)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6842)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
at androidx.navigation.ui.ActionBarOnDestinationChangedListener.setTitle(ActionBarOnDestinationChangedListener.java:47)
at androidx.navigation.ui.AbstractAppBarOnDestinationChangedListener.onDestinationChanged(AbstractAppBarOnDestinationChangedListener.java:100)
at androidx.navigation.NavController.addOnDestinationChangedListener(NavController.java:217)
at androidx.navigation.ui.NavigationUI.setupActionBarWithNavController(NavigationUI.java:219)
at com.smartonet.project.ui.main.activity.MainActivity.initView(MainActivity.java:39)
at com.smartonet.project.core.basic.BaseActivity.onCreate(BaseActivity.java:41)
at android.app.Activity.performCreate(Activity.java:7066)
at android.app.Activity.performCreate(Activity.java:7057)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1216)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2931)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3061)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1747)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6842)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
结局
找了很久答案不明白,其实还是懂得少了,经过对比出现了两个问题
- 我使用的抽屉布局需要使用AppCompatActivity而不是普通的Activity,路径为androidx.appcompat.app.AppCompatActivity
- 我使用的AppTheme从NoActionBar变为了DarkActionBar,导致标题栏一直为空。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
- 出错位置与代码