我创建了一个Activity来测试一些名为Parse的东西。 我尝试过5次活动。我用不同的名称重新创建了活动,重写了Intent的东西并更改了运行代码的按钮。所有的故事都发生了。
这是我的代码:
Intent intent = new Intent(Login.this,Parse.class);
startActivity(intent);
我得到以下LogCat:
11-10 10:26:27.211 17518-17518/? I/art: Late-enabling -Xcheck:jni
11-10 10:26:27.427 17518-17556/rafaelmarinho.dobau D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
11-10 10:26:27.441 17518-17518/rafaelmarinho.dobau D/Atlas: Validating map...
11-10 10:26:27.543 17518-17518/rafaelmarinho.dobau D/AndroidRuntime: Shutting down VM
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: FATAL EXCEPTION: main
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: Process: rafaelmarinho.dobau, PID: 17518
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{rafaelmarinho.dobau/rafaelmarinho.dobau.Parse}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:155)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5343)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at rafaelmarinho.dobau.Parse.onCreate(Parse.java:12)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6010)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:155)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5343)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
11-10 10:26:27.545 17518-17518/rafaelmarinho.dobau E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
11-10 10:26:29.382 17518-17518/rafaelmarinho.dobau I/Process: Sending signal. PID: 17518 SIG: 9
那么我该怎么办才能解决这个问题?
答案 0 :(得分:1)
每当您尝试禁用主页按钮或在Actionbar上执行任何操作时,都应检查getSupportActionbar是否为空,因为这取决于应用程序主题。
if(getSupportActionBar()!=null){
setDisplayShowTitleEnabled(false);
}