简体   繁体   English

加载微调器在 Android 中不起作用

[英]Load Spinner is not working in Android

I develop an Android App.我开发了一个Android应用程序。 One module is to retrieve data from remote server on button click.一个模块是通过单击按钮从远程服务器检索数据。 While retreiving it should display progress spin and After retrieving it should disappear.检索时应显示进度旋转,检索后应消失。 The code is given below代码如下

  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();
    }
}

The Error i got is given below我得到的错误如下

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)

The leak comes probably from your PixelRainActivity.staticThis attribute.泄漏可能来自您的 PixelRainActivity.staticThis 属性。 If you're keeping a reference to an activity, even after that this activity has been destroyed, you have a memory leak.如果您保留对活动的引用,即使在此活动已被销毁之后,也会发生内存泄漏。

The easiest way to fix is to use the application's Context instead.最简单的修复方法是改用应用程序的上下文。 Change your staticThis = this line in the method onCreate() to staticThis = this.getApplicationContext() and it should work (and change the type of staticThis to Context if this is not already the case)将方法 onCreate() 中的 staticThis = this 行更改为 staticThis = this.getApplicationContext() 并且它应该可以工作(如果还不是这种情况,请将 staticThis 的类型更改为 Context )

show progress dialog显示进度对话框

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

hide progress dialog隐藏进度对话框

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

You are creating progress dialog in wrong manner, you need to create it like below.您正在以错误的方式创建进度对话框,您需要像下面那样创建它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM