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;
}
}
}
Try changing your
onChanged()method like this:And then add this to the fragments
onPause()method: