Kelvin Kellner Kelvin Kellner - 4 months ago 16
Java Question

Why is my app crashing when I set DisplayHomeAsUpEnabled to true?

Each time I run this activity my app crashes right away. The app runs fine if I remove:

ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);


Here's my activity's class code:

public class WebActivity extends AppCompatActivity {
private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.example.com/");
mWebView.setWebViewClient(new MyAppWebViewClient());
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}

public boolean onOptionsItemSelected(MenuItem item){
super.onBackPressed();
return true;
}
}


Edit:
I checked my logcat and this was the error message:

08-03 09:06:53.952 4650-4650/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: ca.davesautoservice.davesautoservice, PID: 4650
java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.davesautoservice.davesautoservice/ca.davesautoservice.davesautoservice.WebActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at ca.davesautoservice.davesautoservice.WebActivity.onCreate(WebActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5343)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5333) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
at dalvik.system.NativeStart.main(Native Method)


I personally don't see anything particularly interesting here. The only this I notice is that when it says:

Caused by: java.lang.NullPointerException
at ca.davesautoservice.davesautoservice.WebActivity.onCreate(WebActivity.java:24)


It confirms that it is the line initiating the actionbar that is the problem.

Answer

Try to import android.support.v7.app.ActionBar

and use:

mActionBar = getSupportActionBar();

Make sure that you have minimum api level in your manifest file is above 11. Because the package android.support.v7.app.ActionBar is only supported in api level below 11. Go to manifest and change API version.