当Web View oritentation更改然后按下后退按钮时,应用程序崩溃

时间:2017-11-20 17:43:46

标签: java android nullpointerexception android-webview

我正在开发一个Android应用程序,并注意到我的WebViewerActivity的一个小错误。当我通过Web View打开URL然后更改设备的方向时,方向会相应更改。但是,当我按下Android设备上的后退按钮时,它会导致应用程序崩溃。我不确定是什么导致了这个问题,并希望得到任何建议。

当方向未更改时,后退按钮可以正常运行。

WebViewerActivity

public class WebViewerActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_viewer);
    final TextView setHeader = (TextView) findViewById(R.id.directoryHeader);
    Intent intent = getIntent();

    setHeader.setText(intent.getStringExtra("header"));
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    WebView webView = (WebView) findViewById(R.id.webview);
    webView.setWebViewClient(new WebViewClient());
    webView.setInitialScale(1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setScrollbarFadingEnabled(false);
    webView.loadUrl(intent.getStringExtra("url"));

    intent.removeExtra("url");
    intent.removeExtra("header");
}

 @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }
}

AndroidManifest

<activity
        android:name=".activities.WebViewerActivity"
        android:label="@string/title_activity_web_viewer"
        android:theme="@style/AppTheme.NoActionBar"
        android:configChanges="orientation|screenSize|keyboardHidden"/>

异常

11-20 18:50:24.612 23122-23122/com.russwilkie.metrostatemobile E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.russwilkie.metrostatemobile/com.russwilkie.metrostatemobile.activities.ListViewLinkActivity}: java.lang.NullPointerException
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
                                                                                 at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
                                                                                 at android.app.ActivityThread.access$700(ActivityThread.java:141)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                 at android.os.Looper.loop(Looper.java:137)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                                 at dalvik.system.NativeStart.main(Native Method)
                                                                              Caused by: java.lang.NullPointerException
                                                                                 at com.russwilkie.metrostatemobile.activities.ListViewLinkActivity.setListViewValues(ListViewLinkActivity.java:74)
                                                                                 at com.russwilkie.metrostatemobile.activities.ListViewLinkActivity.onCreate(ListViewLinkActivity.java:41)
                                                                                 at android.app.Activity.performCreate(Activity.java:5104)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
                                                                                 at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692) 
                                                                                 at android.app.ActivityThread.access$700(ActivityThread.java:141) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                 at android.os.Looper.loop(Looper.java:137) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5041) 
                                                                                 at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                 at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                                                 at dalvik.system.NativeStart.main(Native Method) 

0 个答案:

没有答案