如果未启用 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/