gpt4 book ai didi

java - Android 中的运行时异常 AlertDialog

转载 作者:行者123 更新时间:2023-12-01 16:38:20 25 4
gpt4 key购买 nike

我尝试执行以下代码...但 AlertDialog 产生了问题!如果没有这个,代码就可以正常工作。我希望弹出一个对话框并要求用户在 GPS 关闭时启用它!

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 Firstdroid.Gps.Testing@462a35d0 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

最佳答案

无法从Service(与Activity相对)显示对话框。

摘自Dialog文档找到here

A dialog is always created and displayed as a part of an Activity. You should normally create dialogs from within your Activity's onCreateDialog(int) callback method.

事实上,从 Service 显示对话框违反了 Android UI guidelines 。该指南建议改用通知。

如果您确实需要,可以启动一个看起来和行为都类似于DialogActivity,描述为here ,但仍然不明智,因为您可能会在用户正在进行另一项 Activity 时打断他/她。

关于java - Android 中的运行时异常 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6935956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com