0

私は ProgressDialog をオンにする AsyncTasc を持っていますが、すべて正常に動作しますが、しばらくすると異常な動作がアプリに失敗します。私は、そのユーザーが決してそのように振る舞わないことを知っています。しかし、良いテストを行うには、すべてを試す必要があります。

コード:

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {
    ProgressDialog mProgressDialog;
    GetSubs gs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mProgressDialog = new ProgressDialog(MainActivity.this);
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setOnCancelListener(new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                Log.d("Called on Cancel", " Listener");
                gs.cancel(false);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void Clicked(View view) {
        gs = new GetSubs();
        gs.execute();
    }

    public class GetSubs extends AsyncTask<Void, Void, String> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            mProgressDialog.show();
            Log.d("onPreExecute", " in AsyncTasc");
        }

        @Override
        protected String doInBackground(Void... params) {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (isCancelled()) break;
            }
            return null;
        }

        @Override
        protected void onCancelled() {
            // TODO Auto-generated method stub
            super.onCancelled();
            Log.d("onCancelled", " in AsyncTasc");
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            mProgressDialog.dismiss();
            Log.d("onPostExecute", " in AsyncTasc");

        }

    }

}

ログキャット:

04-16 21:14:21.053: E/AndroidRuntime(28767): FATAL EXCEPTION: main
04-16 21:14:21.053: E/AndroidRuntime(28767): java.lang.IllegalArgumentException: View not attached to window manager
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:751)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:474)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:163)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.app.Dialog.dismissDialog(Dialog.java:348)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.app.Dialog$1.run(Dialog.java:139)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.app.Dialog.dismiss(Dialog.java:333)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at com.example.getusersubs.MainActivity$GetSubs.onPostExecute(MainActivity.java:82)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at com.example.getusersubs.MainActivity$GetSubs.onPostExecute(MainActivity.java:1)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.os.AsyncTask.finish(AsyncTask.java:602)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.os.Looper.loop(Looper.java:154)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.app.ActivityThread.main(ActivityThread.java:4945)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at java.lang.reflect.Method.invokeNative(Native Method)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at java.lang.reflect.Method.invoke(Method.java:511)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at dalvik.system.NativeStart.main(Native Method)

なにが問題ですか?回答ありがとうございます

4

1 に答える 1