取消 ProgressDialog 以返回另一个活动时出现 NullPointerException
有人知道这里发生了什么事吗?有没有什么方法可以让用户取消 ProgressDialog 并返回到 Activity A 而不会发生任何崩溃?
重现此崩溃的步骤:
在活动A中,我启动一个新的意图活动B选项卡主机,加载“class1.java”(带有进度对话框)在进度对话框加载完成之前,我快速点击后退按钮返回到活动A,然后我崩溃了
应用程序不会崩溃,如果:
我等到 webview 加载完成然后继续返回活动 A
我取消 ProgressDialog 并停留在 Web 视图上,直到页面加载完成并返回活动 A
第 51 行: ProgressDialog.incrementProgressBy(progress);
Logcat: http://pastebin.com/uBw4xcaY
java.lang.NullPointerException
at com.domain.package.ui.class1$class1_$1.onProgressChanged(class1.java:51)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:358)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
这是我的代码:
[...]
public class class1 extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_stack);
}
public static class class1_ extends Fragment {
/** The Fragment's UI **/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.class_, container, false);
WebView engine = (WebView) v.findViewById(R.id.web_engine);
final ProgressDialog progressDialog;
progressDialog = new ProgressDialog(getActivity());
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.setProgress(0);
progressDialog.show();
engine.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressDialog.setProgress(0);
getActivity().setProgress(progress * 1000);
progressDialog.incrementProgressBy(progress);
if (progress == 100 && progressDialog.isShowing())
progressDialog.dismiss();
}
});
engine.getSettings().setBuiltInZoomControls(false);
engine.getSettings().setUseWideViewPort(false);
engine.getSettings().setSupportZoom(false);
engine.loadUrl("file:///android_asset/index.html");
Toast.makeText(getActivity(),
"Hello this is a toast",
Toast.LENGTH_LONG).show();
return v;
}
}
}
Anyone know what is going on at here? Is there any way to allow user to cancel the progressDialog and go back to Activity A without having any crash ?
Steps to reproduce this crash :
In an activity A and I start a new intent Activity B tabhost that load "class1.java" (Webview with progressDialog) before the progressDialog load finish , i quicky hit the back button to go back to Activity A and i got a crash
The apps won't crash if :
I wait till the webview load finish and go back to Activity A
I cancel the progressDialog and stay on the webview till the page load finish and go back to Activity A
Line 51 : progressDialog.incrementProgressBy(progress);
Logcat : http://pastebin.com/uBw4xcaY
java.lang.NullPointerException
at com.domain.package.ui.class1$class1_$1.onProgressChanged(class1.java:51)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:358)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
Here's my code :
[...]
public class class1 extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_stack);
}
public static class class1_ extends Fragment {
/** The Fragment's UI **/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.class_, container, false);
WebView engine = (WebView) v.findViewById(R.id.web_engine);
final ProgressDialog progressDialog;
progressDialog = new ProgressDialog(getActivity());
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.setProgress(0);
progressDialog.show();
engine.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressDialog.setProgress(0);
getActivity().setProgress(progress * 1000);
progressDialog.incrementProgressBy(progress);
if (progress == 100 && progressDialog.isShowing())
progressDialog.dismiss();
}
});
engine.getSettings().setBuiltInZoomControls(false);
engine.getSettings().setUseWideViewPort(false);
engine.getSettings().setSupportZoom(false);
engine.loadUrl("file:///android_asset/index.html");
Toast.makeText(getActivity(),
"Hello this is a toast",
Toast.LENGTH_LONG).show();
return v;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试像这样更改您的
onChanged()
方法:然后将其添加到片段
onPause()
方法中:Try changing your
onChanged()
method like this:And then add this to the fragments
onPause()
method:当您取消
Dialog
并返回时,Dialog
将设置为null
,但您的WebChromeClient
回调仍在加载并且当进度更改时,在Dialog
上设置进度,但Dialog
为null
,因此导致NullPointerException
。将您的setWebChromeClient()
修改为。When you cancel
Dialog
and go back, theDialog
is set tonull
but yourWebChromeClient
callback is still loading and when progress is changed, progress is set onDialog
, but theDialog
isnull
so causeNullPointerException
. Modify yoursetWebChromeClient()
as.