2

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.

Kelvin Kellner
  • 746
  • 1
  • 6
  • 15

2 Answers2

1

Because used AppCompatActivity, if you want to use ActionBar, you should use this way:

mActionBar = getSupportActionBar();
John Smith
  • 7,243
  • 6
  • 49
  • 61
Luoxiang
  • 11
  • 1
  • When I use `ActionBar mActionBar = getSupportActionBar(); mActionBar.setDisplayHomeAsUpEnabled(true);` I get an error on the first line "Incompatible types. Required: android.app.actionbar Found: android.supprt.v7.app.actionbar" – Kelvin Kellner Aug 03 '16 at 12:54
  • don't use `android.app.actionbar` , should use `android.supprt.v7.app.actionbar` , use this package. `import android.support.v7.app.ActionBar;` – Luoxiang Aug 04 '16 at 07:08
1

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.

xxx
  • 3,315
  • 5
  • 21
  • 40
  • 1
    Thank you, this worked perfectly. I just changed the import from `android.app.actionbar` to `android.support.v7.app.actionbar`. – Kelvin Kellner Aug 03 '16 at 13:53