1

我需要在异步任务运行时向用户显示微调器:

public class RecoverListTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog spinner;
private Context context;

public RecoverListTask(Context c) {
    context = c;
    spinner = new ProgressDialog(context); // spinner
}

@Override
protected void onPreExecute() {
    // show progress spinner
    spinner.setMessage("Downloading Podcats List");
    spinner.show();

        //do something
}

    @Override
protected void onPostExecute(Void v) {
    spinner.dismiss();
}
}

(我只粘贴了有关微调器的部分)但是当我尝试运行应用程序时,出现此错误:

11-05 18:02:49.077: E/WindowManager(28986): Activity com.example.podcast610downloader.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4264ef28 V.E..... R....... 0,0-684,192} that was originally added here
11-05 18:02:49.077: E/WindowManager(28986): android.view.WindowLeaked: Activity com.example.podcast610downloader.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4264ef28 V.E..... R....... 0,0-684,192} that was originally added here
11-05 18:02:49.077: E/WindowManager(28986):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:345)
11-05 18:02:49.077: E/WindowManager(28986):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
11-05 18:02:49.077: E/WindowManager(28986):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
11-05 18:02:49.077: E/WindowManager(28986):     at android.app.Dialog.show(Dialog.java:281)
11-05 18:02:49.077: E/WindowManager(28986):     at com.example.podcast610downloader.RecoverListTask.onPreExecute(RecoverListTask.java:31)
11-05 18:02:49.077: E/WindowManager(28986):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
11-05 18:02:49.077: E/WindowManager(28986):     at android.os.AsyncTask.execute(AsyncTask.java:534)
11-05 18:02:49.077: E/WindowManager(28986):     at com.example.podcast610downloader.MainActivity.onCreate(MainActivity.java:24)
11-05 18:02:49.077: E/WindowManager(28986):     at android.app.Activity.performCreate(Activity.java:5133)
11-05 18:02:49.077: E/WindowManager(28986):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-05 18:02:49.077: E/WindowManager(28986):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-05 18:02:49.077: E/WindowManager(28986):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-05 18:02:49.077: E/WindowManager(28986):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-05 18:02:49.077: E/WindowManager(28986):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-05 18:02:49.077: E/WindowManager(28986):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 18:02:49.077: E/WindowManager(28986):     at android.os.Looper.loop(Looper.java:137)
11-05 18:02:49.077: E/WindowManager(28986):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-05 18:02:49.077: E/WindowManager(28986):     at java.lang.reflect.Method.invokeNative(Native Method)
11-05 18:02:49.077: E/WindowManager(28986):     at java.lang.reflect.Method.invoke(Method.java:525)
11-05 18:02:49.077: E/WindowManager(28986):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-05 18:02:49.077: E/WindowManager(28986):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-05 18:02:49.077: E/WindowManager(28986):     at dalvik.system.NativeStart.main(Native Method)

创建代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //recover list of available podcast (with details)
    RecoverListTask task = new RecoverListTask(this);
    task.execute(null, null, null);

    listView = (ListView) findViewById(R.id.listView);
    arrayAdapter=new ArrayAdapter<PodcastFile>(this, android.R.layout.simple_list_item_1);
    listView.setAdapter(arrayAdapter);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

        }

    });

}

有什么问题?

4

2 回答 2

0

不要保留对上下文变量的引用。这显然是无用的,这就是保留您的活动实例的原因。

只需删除这些行:

private Context context;
context = c;

编辑_ _ __ _ __ _ ___

好的,我会这样做:

public class MainActivity extends Activity
{
    private ProgressDialog  spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner = new ProgressDialog(this);

        // show progress spinner
        spinner.setMessage("Downloading Podcats List");
        spinner.show();

        // recover list of available podcast (with details)
        RecoverListTask task = new RecoverListTask();
        task.execute(null, null, null);
    }

    private void onLoadingComplete()
    {
        spinner.dismiss();
    }

    public class RecoverListTask extends AsyncTask<Void, Void, Void>
    {
        public RecoverListTask()
        {}

        @Override
        protected void onPreExecute()
        {

        }

        @Override
        protected void onPostExecute(Void v)
        {
            onLoadingComplete();
        }

        @Override
        protected Void doInBackground(Void... params)
        {
            // do something

            //If you want to get a reference to the Activity use 
            //MainActivity.this instead of context

            return null;
        }
    }
}
于 2013-11-05T17:20:48.023 回答
0

我认为在实例化时Asynctask你正在传递上下文getApplicationContext()

new RecoverListTask(getApplicationContext()).execute();

而是尝试这样做

 new RecoverListTask(MainActivity.this).execute();

检查它是否有效!

于 2013-11-05T17:57:20.570 回答