我正在使用以下代码在webview中显示progressDialog。 通常情况下每件事都运行正常但是在加载进度对话框的同时按下后退按钮并再次使用webview进行相同的活动它不会立即显示进度对话框,但过了一会儿并重复这两次应用程序崩溃。
同时在onPageStarted上显示_dialog.show()。我见过帖子
ProgressDialog created from onCreateDialog stops animating on second run
但没有用。
任何人指导我这个问题的解决方案是什么?
private static final int DIALOG_WEBVIEW = 0;
private WebView _webView;
private ProgressDialog _dialog;
oncreate()
{
_webView.canGoBack();
_webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
showDialog(DIALOG_WEBVIEW); // crashes here
}
@Override
public void onPageFinished(WebView view, String url) {
if(_dialog.isShowing())
{
removeDialog(DIALOG_WEBVIEW);
}
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
if (Constants.LOG)Log.d("recieved error-------------------------", "");
super.onReceivedError(view, errorCode, description, failingUrl);
if(_dialog.isShowing())
{
removeDialog(DIALOG_WEBVIEW);
}
}
});
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DIALOG_WEBVIEW:
_dialog.setMessage(Constants.TEXT_PLEASE_WAIT);
_dialog.setCancelable(true);
return;
default:
return ;
}
}
@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DIALOG_WEBVIEW:
{
_dialog = new ProgressDialog(this);
_dialog.show();
return _dialog;
}
default:
return null;
}
}
log cat
07-15 11:43:16.744: ERROR/AndroidRuntime(750): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@406b7138 is not valid; is your activity running?
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at android.view.ViewRoot.setView(ViewRoot.java:527)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at android.view.Window$LocalWindowManager.addView(Window.java:424)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at android.app.Dialog.show(Dialog.java:241)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at com.android.mobile.modules.volunteer.VolunteerScreenNew.onCreateDialog(VolunteerScreenNew.java:343)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at android.app.Activity.onCreateDialog(Activity.java:2482)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at android.app.Activity.createDialog(Activity.java:882)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at android.app.Activity.showDialog(Activity.java:2557)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at android.app.Activity.showDialog(Activity.java:2524)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at com.android.mobile.modules.volunteer.VolunteerScreenNew$1.onPageStarted(VolunteerScreenNew.java:181)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:264)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at android.os.Looper.loop(Looper.java:123)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at java.lang.reflect.Method.invoke(Method.java:507)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-15 11:43:16.744: ERROR/AndroidRuntime(750): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
通常我们可以使用以下代码覆盖后退按钮操作
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if ( keyCode == KeyEvent.KEYCODE_BACK&& event.getRepeatCount() == 0)
{
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
创建一个方法onBackPressed()并关闭该对话框并启动anather活动。