Android:AsyncTask ProgressDialog坏令牌窗口?

时间:2013-03-28 15:02:26

标签: android

您好,我在尝试显示进度对话框时收到错误的令牌窗口错误

下面的

是我执行AsyncTask

的活动的onCreate中的处理程序
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progress = new ProgressDialog(getApplicationContext());
        progress.setTitle(R.string.progress_title);
        mHandler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                Log.d("", "Handling callback");
                super.handleMessage(msg);
                position = (Position)msg.obj;

                Toast.makeText(getApplicationContext(), position.getLattitude() + " " + position.getLongetude(),
                    Toast.LENGTH_LONG).show();

                userLocation.stopSearchLocation();

                FindRestaurants findRestaurants = new FindRestaurants();
                findRestaurants.execute();
            }

        };
}

我的AsyncTask

class FindRestaurants extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            // get users current location

            progress.show(); //fails here

            Log.d("", "onPreExecute");
            if (position == null) {

                position = userLocation.getLastKnownLocation();
                Log.d("", "position = " + position.getLattitude() + " " + position.getLongetude());
            }

            // find upper and lower boundaries
            Log.d("", "position outside of condition= " + position.getLattitude() + " " + position.getLongetude());

            super.onPreExecute();
        }

完整堆栈跟踪

03-28 14:40:02.314: E/AndroidRuntime(13658): FATAL EXCEPTION: main
03-28 14:40:02.314: E/AndroidRuntime(13658): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
03-28 14:40:02.314: E/AndroidRuntime(13658):    at android.view.ViewRoot.setView(ViewRoot.java:532)
03-28 14:40:02.314: E/AndroidRuntime(13658):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
03-28 14:40:02.314: E/AndroidRuntime(13658):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
03-28 14:40:02.314: E/AndroidRuntime(13658):    at android.app.Dialog.show(Dialog.java:241)
03-28 14:40:02.314: E/AndroidRuntime(13658):    at android.app.ProgressDialog.show(ProgressDialog.java:107)
03-28 14:40:02.314: E/AndroidRuntime(13658):    at android.app.ProgressDialog.show(ProgressDialog.java:90)
03-28 14:40:02.314: E/AndroidRuntime(13658):    at android.app.ProgressDialog.show(ProgressDialog.java:85)
03-28 14:40:02.314: E/AndroidRuntime(13658):    at com.jr.haliotest.MainActivity$FindRestaurants.onPreExecute(MainActivity.java:61)
03-28 14:40:02.314: E/AndroidRuntime(13658):    at android.os.AsyncTask.execute(AsyncTask.java:391)
03-28 14:40:02.314: E/AndroidRuntime(13658):    at com.jr.haliotest.MainActivity$1.handleMessage(MainActivity.java:138)
03-28 14:40:02.314: E/AndroidRuntime(13658):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 14:40:02.314: E/AndroidRuntime(13658):    at android.os.Looper.loop(Looper.java:130)
03-28 14:40:02.314: E/AndroidRuntime(13658):    at android.app.ActivityThread.main(ActivityThread.java:3835)
03-28 14:40:02.314: E/AndroidRuntime(13658):    at java.lang.reflect.Method.invokeNative(Native Method)
03-28 14:40:02.314: E/AndroidRuntime(13658):    at java.lang.reflect.Method.invoke(Method.java:507)
03-28 14:40:02.314: E/AndroidRuntime(13658):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
03-28 14:40:02.314: E/AndroidRuntime(13658):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
03-28 14:40:02.314: E/AndroidRuntime(13658):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:6)

使用当前活动上下文显示ProgressDialog而不是getApplicationContext()

progress = new ProgressDialog(Your_Current_Activity.this);