2012-02-01 38 views
0

我一直試圖在用戶沒有啓用GPS時使用AlertDialog打開,並使用intent將它們指向Settings.ACIOTN_LOCATION_SOURCE_SETTINGS當想要顯示對話框時,App force關閉

private void buildAlertDialog() 
{ 
    // TODO Auto-generated method stub 
    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); 
    builder.setMessage("Gps is disabled, do you want to enable it?"); 
    builder.setCancelable(false); 
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int id) 
     { 
      // TODO Auto-generated method stub 
      startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 

     } 
    }); 

    builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int id) { 
      // TODO Auto-generated method stub 
      dialog.cancel(); 
     } 
    }); 
    AlertDialog alert = builder.create(); 
    alert.show(); 
} 

buildAlertDialog()是當用戶輸入該活動和GSP未啓用它是由這個片段的代碼稱爲稱爲

mLocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    if(!mLocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
    { 
     buildAlertDialog(); 
    } 
    mLocListener = new LocListener(); 
    mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocListener); 

錯誤的logcat的輸出如下

FATAL EXCEPTION: main 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fyp.run_race/com.fyp.run_race.Begin_Run}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797) 
at android.app.ActivityThread.access$2300(ActivityThread.java:135) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:143) 
at android.app.ActivityThread.main(ActivityThread.java:4914) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:521) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
at android.view.ViewRoot.setView(ViewRoot.java:513) 
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
at android.app.Dialog.show(Dialog.java:241) 
at com.fyp.run_race.Begin_Run.buildAlertDialog(Begin_Run.java:133) 
at com.fyp.run_race.Begin_Run.onCreate(Begin_Run.java:82) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745) 
... 11 more 

我認爲這可能是新的活動沒有註冊的應用程序的Android清單文件

+0

請嘗試。 builder = new AlertDialog.Builder(YouActivityName.this); – Arslan 2012-02-01 18:38:20

回答

1

有可能與getApplicationContext()線路有問題,檢查this thread

你應該傳遞一個Activity參考builderAlertDialog()這將是this如果該方法是從Activity類調​​用。

+0

非常感謝。 – 2012-02-01 18:39:02

相關問題