我有一个AsyncTasc转向ProgressDialog,一切都很好,但经过一段时间的异常行为应用程序失败。我知道,该用户永远不会这样做。但是要进行良好的测试是必要的。
代码:
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
ProgressDialog mProgressDialog;
GetSubs gs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
Log.d("Called on Cancel", " Listener");
gs.cancel(false);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void Clicked(View view) {
gs = new GetSubs();
gs.execute();
}
public class GetSubs extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mProgressDialog.show();
Log.d("onPreExecute", " in AsyncTasc");
}
@Override
protected String doInBackground(Void... params) {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (isCancelled()) break;
}
return null;
}
@Override
protected void onCancelled() {
// TODO Auto-generated method stub
super.onCancelled();
Log.d("onCancelled", " in AsyncTasc");
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
mProgressDialog.dismiss();
Log.d("onPostExecute", " in AsyncTasc");
}
}
}
logcat的:
04-16 21:14:21.053: E/AndroidRuntime(28767): FATAL EXCEPTION: main
04-16 21:14:21.053: E/AndroidRuntime(28767): java.lang.IllegalArgumentException: View not attached to window manager
04-16 21:14:21.053: E/AndroidRuntime(28767): at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:751)
04-16 21:14:21.053: E/AndroidRuntime(28767): at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:474)
04-16 21:14:21.053: E/AndroidRuntime(28767): at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:163)
04-16 21:14:21.053: E/AndroidRuntime(28767): at android.app.Dialog.dismissDialog(Dialog.java:348)
04-16 21:14:21.053: E/AndroidRuntime(28767): at android.app.Dialog$1.run(Dialog.java:139)
04-16 21:14:21.053: E/AndroidRuntime(28767): at android.app.Dialog.dismiss(Dialog.java:333)
04-16 21:14:21.053: E/AndroidRuntime(28767): at com.example.getusersubs.MainActivity$GetSubs.onPostExecute(MainActivity.java:82)
04-16 21:14:21.053: E/AndroidRuntime(28767): at com.example.getusersubs.MainActivity$GetSubs.onPostExecute(MainActivity.java:1)
04-16 21:14:21.053: E/AndroidRuntime(28767): at android.os.AsyncTask.finish(AsyncTask.java:602)
04-16 21:14:21.053: E/AndroidRuntime(28767): at android.os.AsyncTask.access$600(AsyncTask.java:156)
04-16 21:14:21.053: E/AndroidRuntime(28767): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
04-16 21:14:21.053: E/AndroidRuntime(28767): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 21:14:21.053: E/AndroidRuntime(28767): at android.os.Looper.loop(Looper.java:154)
04-16 21:14:21.053: E/AndroidRuntime(28767): at android.app.ActivityThread.main(ActivityThread.java:4945)
04-16 21:14:21.053: E/AndroidRuntime(28767): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 21:14:21.053: E/AndroidRuntime(28767): at java.lang.reflect.Method.invoke(Method.java:511)
04-16 21:14:21.053: E/AndroidRuntime(28767): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-16 21:14:21.053: E/AndroidRuntime(28767): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-16 21:14:21.053: E/AndroidRuntime(28767): at dalvik.system.NativeStart.main(Native Method)
有什么问题?谢谢你的回答
答案 0 :(得分:0)
变化:
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
mProgressDialog.dismiss();
Log.d("onPostExecute", " in AsyncTasc");
}
到
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(mProgressDialog != null && mProgressDialog.isShowing()){
mProgressDialog.dismiss();
}
Log.d("onPostExecute", " in AsyncTasc");
}
问题在于您试图在不检查是否显示进度对话框的情况下解除进度对话框,用户可以通过按backKey
轻松取消该对话框。
您仍然可以通过将Cancellable设置为false来强制用户等待对话框。这样后面的键就没有效果了。