我有这种方法,我想创建一个AlertDialog,但当我调用它时,应用程序到达ad.show时停止工作
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is necessary for Traffic Finder");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
//mContext.startActivity(intent);
startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// Create Alert Dialog
AlertDialog ad= alertDialog.create();
// Showing Alert Message
ad.show();
}
我明白了:
08-26 20:47:11.462 28940-29005/? E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[CoordinatesService]
Process: com.example.manos.trafficfinder, PID: 28940
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:566)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:282)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:298)
at com.example.manos.trafficfinder.GPSTracker.showSettingsAlert(GPSTracker.java:182)
at com.example.manos.trafficfinder.GeographicCoordinates.getLatitudeString(GeographicCoordinates.java:32)
at com.example.manos.trafficfinder.DataManagement.addPosition(DataManagement.java:116)
at com.example.manos.trafficfinder.CoordinatesService.onHandleIntent(CoordinatesService.java:28)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)