我有2个页面,我想要的是如果用户点击第一页的按钮,它将显示ProgressDialog并转到第二页,如果用户单击后退按钮继续到第一页,它将不会显示ProgressDialog我试试
Btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog = ProgressDialog.show(MainActivity.this, "",
"Loading. Please wait...", true);
dialog.setMax(2);
Intent statPage = new Intent(MainActivity.this, Statistic.class);
startActivity(statPage);
}
});
public void onResume() {
if (dialog.isShowing()) {
dialog.dismiss();
}
super.onResume();
}
但是强迫关闭
05-09 10:02:54.520: ERROR/AndroidRuntime(623): FATAL EXCEPTION: main
05-09 10:02:54.520:ERROR / AndroidRuntime(623):java.lang.RuntimeException:无法恢复活动{android.test.cal/android.test.cal.MainActivity}:java.lang.NullPointerException 05-09 10:02:54.520:ERROR / AndroidRuntime(623):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128) 05-09 10:02:54.520:ERROR / AndroidRuntime(623):在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143) 05-09 10:02:54.520:ERROR / AndroidRuntime(623):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684) 05-09 10:02:54.520:ERROR / AndroidRuntime(623):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125) 05-09 10:02:54.520:ERROR / AndroidRuntime(623):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033) 05-09 10:02:54.520:ERROR / AndroidRuntime(623):在android.os.Handler.dispatchMessage(Handler.java:99) 05-09 10:02:54.520:ERROR / AndroidRuntime(623):在android.os.Looper.loop(Looper.java:123) 05-09 10:02:54.520:ERROR / AndroidRuntime(623):在android.app.ActivityThread.main(ActivityThread.java:4627) 05-09 10:02:54.520:ERROR / AndroidRuntime(623):at java.lang.reflect.Method.invokeNative(Native Method) 05-09 10:02:54.520:ERROR / AndroidRuntime(623):at java.lang.reflect.Method.invoke(Method.java:521) 05-09 10:02:54.520:ERROR / AndroidRuntime(623):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 05-09 10:02:54.520:ERROR / AndroidRuntime(623):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 05-09 10:02:54.520:ERROR / AndroidRuntime(623):at dalvik.system.NativeStart.main(Native Method) 05-09 10:02:54.520:ERROR / AndroidRuntime(623):引起:java.lang.NullPointerException 05-09 10:02:54.520:ERROR / AndroidRuntime(623):在android.test.cal.MainActivity.onResume(MainActivity.java:165) 05-09 10:02:54.520:ERROR / AndroidRuntime(623):在android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149) 05-09 10:02:54.520:ERROR / AndroidRuntime(623):在android.app.Activity.performResume(Activity.java:3823) 05-09 10:02:54.520:ERROR / AndroidRuntime(623):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
请帮助
答案 0 :(得分:3)
请在onPause而不是onResume中编写进度对话框代码。当您在当时调用主活动中的另一个活动时,调用onPause并关闭进度对话框。