当Webview方向更改然后按下后退按钮时,应用程序崩溃
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当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)
答案
将此添加到您的AndroidManifest.xml
<activity android:name=".MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait">
它会将您的视图锁定为纵向。所以方向是锁定肖像。
以上是关于当Webview方向更改然后按下后退按钮时,应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章