【发布时间】:2011-07-15 11:53:26
【问题描述】:
我正在使用以下代码在 webview 中显示 progressDialog。 通常一切正常,但是在加载进度对话框时,我按下后退按钮并再次使用 webview 进入相同的活动,它不会立即显示进度对话框,但会在一段时间后重复这两次,三次应用程序崩溃。
同时显示 _dialog.show() onPageStarted。我看过帖子
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;
}
}
日志猫
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)
【问题讨论】:
-
坚持的一件事是您不需要保留对加载对话框的引用。在
onPrepareDialog中使用提供给您的Dialog参数,这将是您在onCreateDialog中返回的任何内容。虽然我不确定这是否会导致不良行为。