当我将DisplayHomeAsUpEnabled设置为true时,为什么我的应用程序崩溃了?

时间:2016-08-03 00:35:08

标签: java android android-studio crash android-actionbar

每次我运行此活动时,我的应用都会立即崩溃。如果删除,应用程序运行正常:

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

这是我的活动的课程代码:

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;
}
}

修改 我检查了我的logcat,这是错误信息:

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)

我个人在这里看不到任何特别有趣的东西。我注意到的唯一一点是它说:

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

它确认启动操作栏的行是问题所在。

2 个答案:

答案 0 :(得分:1)

因为使用了AppCompatActivity,如果你想使用ActionBar,你应该这样使用:

mActionBar = getSupportActionBar();

答案 1 :(得分:1)

尝试导入android.support.v7.app.ActionBar

并使用:

mActionBar = getSupportActionBar();

确保清单文件中的最小api级别高于11.因为软件包android.support.v7.app.ActionBar仅在低于11的api级别中受支持。转到清单并更改API版本。