2011-08-04 45 views
0

我試圖執行以下代碼...但AlertDialog正在創建問題!沒有這個代碼工作正常。我想要一個對話框彈出並要求用戶在GPS關閉時啓用GPS!Android中的運行時異常AlertDialog

public class Testing extends Service { 
    private Location loc; 
    private Dialog dialog; 
    String DATA; 
    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
    @Override 
    public void onCreate() 
    {  
      super.onCreate(); 
    } 
    @Override 
    public void onStart(Intent intent, int startId) 
    { 
     LocationManager LM = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     if (!LM.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage("Your GPS is disabled! Would you like to enable it?") 
        .setCancelable(false).setPositiveButton("Enable GPS", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int id) { 
            Intent gpsOptionsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
            startActivity(gpsOptionsIntent); 
           } 
          }); 
      builder.setNegativeButton("Do nothing", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 
      AlertDialog alert = builder.create(); 
      alert.show(); 
     } 
     LocationListener LL = new MyLocationListener(); 
     LM.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, LL); 
     LM.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, LL); 
     /*//Runnable to determine when the first GPS fix was received. 
     Runnable showWaitDialog = new Runnable() { 
      @Override 
      public void run() { 
      while (loc == null) { 
      // Wait for first GPS Fix (do nothing until loc != null) 
      } 
      // After receiving first GPS Fix dismiss the Progress Dialog 
      dialog.dismiss(); 
      } 
      }; 
      // Create a Dialog to let the User know that we're waiting for a GPS Fix 
      dialog = ProgressDialog.show(getApplicationContext(), "Please wait...","Retrieving GPS data ...", true); 
      Thread t = new Thread(showWaitDialog); 
      t.start();*/ 
     LM.addNmeaListener(new NmeaListener() { 
     public void onNmeaReceived(long timestamp, String nmea) 
     { 
        DATA = nmea; 
     }}); 
    } 

但我得到這個錯誤!註釋行內的ProgressDialog代碼也會引發相同的錯誤!

08-04 04:36:09.664: ERROR/AndroidRuntime(6301): FATAL EXCEPTION: main 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301): java.lang.RuntimeException: Unable to start service [email protected] with Intent { cmp=Firstdroid.Gps/.Testing }: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3282) 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  at android.app.ActivityThread.access$3600(ActivityThread.java:135) 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2211) 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  at android.os.Handler.dispatchMessage(Handler.java:99) 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  at android.os.Looper.loop(Looper.java:144) 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  at android.app.ActivityThread.main(ActivityThread.java:4937) 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  at java.lang.reflect.Method.invoke(Method.java:521) 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  at dalvik.system.NativeStart.main(Native Method) 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  at android.view.ViewRoot.setView(ViewRoot.java:513) 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  at android.app.Dialog.show(Dialog.java:241) 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  at Firstdroid.Gps.UrbanExplorer.onStart(UrbanExplorer.java:84) 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  at android.app.Service.onStartCommand(Service.java:420) 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3267) 
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):  ... 10 more 

回答

3

這是不可能顯示從Service一個對話框(而不是Activity)。

摘自Dialog文檔發現here

始終創建一個對話框並顯示爲Activity的一部分。您通常應該從ActivityonCreateDialog(int)回調方法中創建對話框。

實際上,顯示來自Service的對話框違反了Android UI guidelines。指南建議使用通知。

如果你真的需要,你可以啓動Activity,看上去就像一個Dialog,被描述here,但仍不明智的,你可能會打擾用戶,當他/她在另一箇中間活動。

2

你不應該試圖從Service推出Dialog。檢查供應商是否從Activity啓用,並且如果不創建Dialog。如果它們已啓用,請啓動Service

1

您無法從服務打開對話框,因此您必須將對話作爲活動,並且必須從服務中啓動該活動。 有關對話活動詳情,請參見該問題

android:chat app popup view