android - 使用 AlertDialog 将用户带到设置页面时出现空异常

标签 android android-alertdialog setting

如果未启用 GPS,我将使用下面的方法将用户带到设置页面。但它抛出异常 null

public void showSettingsAlert() {
    try {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

        alertDialog.setTitle("GPS Settings");

        alertDialog.setMessage("Please enable GPS. Do you want to go to settings menu?");

        // On pressing Settings button
        alertDialog.setPositiveButton("Settings",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    mContext.startActivity(intent);
                }
            }
        );

        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            }
        );

        // Showing Alert Message
        alertDialog.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

有人可以帮帮我吗

最佳答案

使用下面一行

AlertDialog alertDialog = alertDialogBuilder.create();

将这一行添加到你的代码中

public void showSettingsAlert() {
    try {
        AlertDialog.Builder alertDialogBuilder= new AlertDialog.Builder(mContext);

        alertDialogBuilder.setTitle("GPS Settings");

        alertDialogBuilder
                .setMessage("Please enable GPS. Do you want to go to settings menu?");

        // On pressing Settings button
        alertDialogBuilder.setPositiveButton("Settings",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(
                                Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        mContext.startActivity(intent);
                    }
                });

        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

//added this line
     AlertDialog alertDialog = alertDialogBuilder.create();

        // Showing Alert Message
        alertDialog.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

关于android - 使用 AlertDialog 将用户带到设置页面时出现空异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20947602/

相关文章:

android - 如何将位图分成也是位图的部分

java - android 自定义actionbar onclicklistener

android - 从另一个类调用对话框

jquery - 在哪里可以找到合适的 jQuery ajax 标题设置文档?

java - 对 Firestore 文档进行排序

android - 设计 Android EditText 以显示谷歌描述的错误消息

android - 防止在 AlertDialog 中自动聚焦 EditText

java - 使用 fragment 上的按钮打开 AlertDialog

c# - C# 应用程序中的自定义用户配置

delphi - 在 delphi 中的事件拨号或 VPN 连接上设置 IE 中的 Internet 代理