安卓报错:java.lang.RuntimeException: Unable to start activity ComponentInfo---getSupportActionBar()为null

报错信息:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.edm, PID: 16919
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.edm/com.example.edm.StudentActivity.TimeTable.ui.editcourse.EditActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3773)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3952)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2386)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8169)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
        at com.example.edm.StudentActivity.TimeTable.ui.editcourse.EditActivity.setActionBar(EditActivity.java:248)
        at com.example.edm.StudentActivity.TimeTable.ui.editcourse.EditActivity.onCreate(EditActivity.java:75)
        at android.app.Activity.performCreate(Activity.java:8086)
        at android.app.Activity.performCreate(Activity.java:8074)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1313)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3746)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3952) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2386) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:213) 
        at android.app.ActivityThread.main(ActivityThread.java:8169) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101) 

错误原因:

private void setActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle(R.string.course_edit);
    }

代码中actionBar为空指针。

解决方案:检查AndroidManifest.xml文件中相关Activity的theme是否为:

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

将其改为:

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

你可能感兴趣的:(Android,android,android,studio,java)