2013-03-28 71 views
0

你好我recieving一個壞的令牌窗口錯誤,當我試圖表明我的進度對話框Android:AsyncTask ProgressDialog壞令牌窗口?

下面

是我的我的活動的onCreate內部處理程序執行的AsyncTask

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

回答

6

使用當前活動上下文來顯示ProgressDialog而不是getApplicationContext()

progress = new ProgressDialog(Your_Current_Activity.this);