问题
In my app, I am calling an async task on a button click and I am using onpreexecute method to start "progress dialog" and onpostexecute to end the same.
I am getting this error while implementing the above - "View not attached to window manager."
Here is my async code -
/**
* Async task class to get json by making HTTP call
*/
private class questionfeed_async extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Hi "+userName+ "! Loading your question feed ");
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (pDialog.isShowing()) {
pDialog.dismiss();
}
}
I There are some solution given in SOF post question. try them too yet did not success.
Logcat -
java.lang.IllegalArgumentException: View not attached to window manager at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:670) at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:351) at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:162) at android.app.Dialog.dismissDialog(Dialog.java:319) at android.app.Dialog.dismiss(Dialog.java:302) at cheerz.fragment.Profile_Fragment$user_detail_async.onPostExecute(Profile_Fragment.java:207) at cheerz.fragment.Profile_Fragment$user_detail_async.onPostExecute(Profile_Fragment.java:149) at android.os.AsyncTask.finish(AsyncTask.java:631) at android.os.AsyncTask.access$600(AsyncTask.java:177) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:153) at android.app.ActivityThread.main(ActivityThread.java:5034) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584) at dalvik.system.NativeStart.main(Native Method)
回答1:
use this code:
if (pDialog != null) {
pDialog.dismiss();
}
Insted of:
if (pDialog.isShowing()) {
pDialog.dismiss();
}
来源:https://stackoverflow.com/questions/28789783/how-to-solve-the-error-view-not-attached-to-window-manager-in-android