简体   繁体   中英

App crashes when Web View oritentation changes then back button is pressed

I am developing an Android Application and am noticing a minor bug with my WebViewerActivity. When I open up a URL through Web View then change the orientation of the device, the orientation changes appropriately. However, when I then pressed the back button on the Android Device, it causes the App to crash. I am not sure what is causing this problem and would appreciate any suggestions.

The back button is able to run fine when the orientation is not changed.

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"/>

Exception

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) 

Added this to your AndroidManifest.xml

<activity android:name=".MainActivity"
            android:configChanges="orientation"
            android:screenOrientation="portrait">

It will lock your view to portrait. So the orientation is lock to Portrait.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM