应用程序在显示对话框时强制关闭

发布于 2025-01-01 06:46:43 字数 3194 浏览 2 评论 0原文

我一直在尝试在用户未启用 GPS 时使用 AlertDialog 打开,并使用意图将其引导至 Settings.ACIOTN_LOCATION_SOURCE_SETTINGS

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

当用户进入此活动且 GSP 未启用时,将调用 buildAlertDialog() ,此代码片段将调用它。

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 清单文件中注册

I've been trying to using an AlertDialog to open when the user does not have GPS enabled and direct them to the Settings.ACIOTN_LOCATION_SOURCE_SETTINGS using an intent.

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

The buildAlertDialog() is called when the user enters this activity and GSP is not enabled it is called by this snippet of code

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

The Logcat output of the error is as follows

 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

I thought it might be that the new activity is not registered with the android manifest file of the application

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

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

发布评论

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

评论(1

入怼 2025-01-08 06:46:43

getApplicationContext() 行可能有问题,请检查 此线程

您应该将 Activity 引用传递给 builderAlertDialog() ,它将是 this如果从 Activity 类调用该方法。

There may be a problem with the getApplicationContext() line, check this thread

You should pass an Activity reference to the builderAlertDialog() it would be this if the method is called from an Activity class.

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