1

asynctask の実行中にユーザーにスピナーを表示する必要があります。

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)

onCreate コード:

@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

Asynctaskコンテキストを渡しているインスタンスを作成しているときに思いますgetApplicationContext()

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

代わりに、このようにしてみてください

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

それが機能するかどうかを確認してください!

于 2013-11-05T17:57:20.570 に答える
0

コンテキスト変数への参照を保持しないでください。それは明らかに役に立たず、それがアクティビティインスタンスを保持するものです。

これらの行を削除するだけです:

private Context context;
context = c;

編集_ _ _ _ _ _ _

OKここに私がそれを行う方法があります:

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 に答える