0
我有一個AsyncTasc輪到ProgressDialog,所有工作都很好,但是在一段時間以後應用程序出現異常行爲失敗。我知道,那個用戶永遠不會這麼做。但爲了全面測試,有必要進行良好的測試。ProgressDialog和AsyncTask異常行爲失敗
代碼:
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)
有什麼不對?感謝您的解答
更改後仍然失敗,但'setCancellable()'解決了我的問題。謝謝你的解釋。 – Bullman