Android 中的运行时异常 AlertDialog

发布于 2024-11-28 04:59:35 字数 5485 浏览 0 评论 0原文

我尝试执行以下代码...但 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

I trying to execute the following code... but the AlertDialog is creating problems! Without that the code works fine. I want a dialogbox to pop up and ask the user to enable GPS when its turned off!

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;
        }});
    }

But I am getting this error! The ProgressDialog code within the comment lines also throws the same error!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蒲公英的约定 2024-12-05 04:59:35

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

此处Dialog文档摘录>

对话框始终作为Activity的一部分创建和显示。您通常应该在 ActivityonCreateDialog(int) 回调方法中创建对话框。

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

如果确实需要,您可以启动一个看起来和行为都像DialogActivity,其描述为此处,但仍然不明智,因为您可能会在用户正在操作时打断他/她在另一项活动中。

It is not possible to show a dialog from a Service (as opposed to Activity).

Excerpt from Dialog documentation found 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.

In fact, showing a dialog from a Service violates the Android UI guidelines. The guidelines recommend using notifications instead.

If you really need to, you can launch an Activity that looks and behaves like a Dialog, which is described here, but still ill-advised as you may be interrupting the user while he/she is in the middle of another activity.

孤寂小茶 2024-12-05 04:59:35

您不应该尝试从 Service 启动 Dialog。检查是否从 Activity 启用了提供程序,如果没有,则创建 Dialog。如果它们已启用,请启动服务

You shouldn't be trying to launch a Dialog from a Service. Check if the providers are enabled from an Activity, and if not create the Dialog. If they are enabled, launch the Service.

嘿嘿嘿 2024-12-05 04:59:35

您无法从服务打开对话框,因此您必须将活动作为对话框创建,并且必须从服务启动该活动。
有关对话框 Activity 的详细信息,请参阅此问题

android:chat app popup view

You can not open a dialog from Service so you have to make a activity as dialog and you have to start that activity from your service.
For details about dialog Activity see this question

android:chat app popup view

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文