Load Spinner在Android中无效

时间:2015-09-09 13:04:33

标签: android

我开发了一个Android应用程序。一个模块是在按钮点击时从远程服务器检索数据。虽然retreiving它应显示进度旋转和检索后它应该消失。代码如下:

  b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            new Load_data().execute();

        }
    });
        }

 private class Load_data extends AsyncTask<Void, Void, Void> {
    ProgressDialog pd;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd= ProgressDialog.show(result.this, "Retreiving", "Please Wait....");
    }

    protected Void doInBackground(Void... param) {
        publishProgress(param);
        butt();
        return null;
    }

    protected void onPostExecute(String arg) {
        pd.dismiss();
    }
}

我得到的错误在下面给出

09-09 18:22:39.422: E/WindowManager(524): android.view.WindowLeaked: Activity com.example.covai.result has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{37ab2005 V.E..... R......D 0,0-684,324} that was originally added here
09-09 18:22:39.422: E/WindowManager(524):   at android.view.ViewRootImpl.<init>(ViewRootImpl.java:422)
09-09 18:22:39.422: E/WindowManager(524):   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:250)
09-09 18:22:39.422: E/WindowManager(524):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
09-09 18:22:39.422: E/WindowManager(524):   at android.app.Dialog.show(Dialog.java:297)
09-09 18:22:39.422: E/WindowManager(524):   at android.app.ProgressDialog.show(ProgressDialog.java:116)
09-09 18:22:39.422: E/WindowManager(524):   at android.app.ProgressDialog.show(ProgressDialog.java:99)
09-09 18:22:39.422: E/WindowManager(524):   at android.app.ProgressDialog.show(ProgressDialog.java:94)
09-09 18:22:39.422: E/WindowManager(524):   at com.example.covai.result$Load_data.onPreExecute(result.java:337)
09-09 18:22:39.422: E/WindowManager(524):   at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
09-09 18:22:39.422: E/WindowManager(524):   at android.os.AsyncTask.execute(AsyncTask.java:535)
09-09 18:22:39.422: E/WindowManager(524):   at com.example.covai.result$3.onClick(result.java:138)
09-09 18:22:39.422: E/WindowManager(524):   at android.view.View.performClick(View.java:4471)
09-09 18:22:39.422: E/WindowManager(524):   at android.view.View$PerformClick.run(View.java:18784)
09-09 18:22:39.422: E/WindowManager(524):   at android.os.Handler.handleCallback(Handler.java:808)
09-09 18:22:39.422: E/WindowManager(524):   at android.os.Handler.dispatchMessage(Handler.java:103)
09-09 18:22:39.422: E/WindowManager(524):   at android.os.Looper.loop(Looper.java:193)
09-09 18:22:39.422: E/WindowManager(524):   at android.app.ActivityThread.main(ActivityThread.java:5330)
09-09 18:22:39.422: E/WindowManager(524):   at java.lang.reflect.Method.invoke(Native Method)
09-09 18:22:39.422: E/WindowManager(524):   at java.lang.reflect.Method.invoke(Method.java:372)
09-09 18:22:39.422: E/WindowManager(524):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
09-09 18:22:39.422: E/WindowManager(524):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)

2 个答案:

答案 0 :(得分:0)

泄漏可能来自您的PixelRainActivity.staticThis属性。如果您保留对某个活动的引用,即使此活动已被销毁,您也会发生内存泄漏。

最简单的修复方法是使用应用程序的Context。将staticThis = onCreate()方法中的这一行更改为staticThis = this.getApplicationContext(),它应该可以工作(如果不是这样,则将staticThis的类型更改为Context)

显示进度对话框

if (pd != null && !pd.isShowing() && !activity.isFinishing(){
    pd.show();
}

隐藏进度对话框

if (pd != null && pd.isShowing()) {
  pd.dismiss();
}

答案 1 :(得分:0)

您正在以错误的方式创建进度对话框,您需要像下面一样创建它。

ProgressDialog pd;
pdia1 = new ProgressDialog(Youractivity.this);
            pdia1.setMessage("Loading Quiz...");
            pdia1.setCancelable(false);
            pdia1.setCanceledOnTouchOutside(false);
            pdia1.show();