0

嗨,当我尝试显示我的进度对话框时,我收到一个错误的令牌窗口错误

下面是我在执行 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();
            }

        };
}

我的异步任务

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)
4

1 回答 1

6

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

progress = new ProgressDialog(Your_Current_Activity.this);
于 2013-03-28T15:06:06.493 回答