我正在努力解决一个我找不到的小问题!在我的主要活动中,我想显示一个AlertDialog并且如果用户不想再次显示该对话框,则有一个复选框。
我可以使用复选框等显示对话框..但出于某种原因,当我尝试将一个OnCheckedChangeListener添加到CheckBox时,我的应用程序在它启动之前关闭:(
以下是我的代码的相关部分:
初始化SharedPreferences的变量:
public final static String WEL_PREF = "Welcome Dialog Preference";
public static int welcomeChecked = 0;
Inside OnCreate:
SharedPreferences welcomePref = getSharedPreferences(WEL_PREF, MODE_PRIVATE);
welcomeChecked = welcomePref.getInt("tut", 0);
...
if(welcomeChecked == 1) {
showAlertDialog();
}
showAlertDialog()函数:
private void showAlertDialog() {
final Intent tutorial = new Intent(MainActivity.this, TutorialActivity.class);
View checkBoxView = View.inflate(this, R.layout.welcome_checkbox, null);
CheckBox welcomeCheck = (CheckBox) findViewById(R.id.tutCheck);
welcomeCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences welcomePref = getSharedPreferences(WEL_PREF, MODE_PRIVATE);
SharedPreferences.Editor editor = welcomePref.edit();
if(isChecked) {
editor.putInt("tut", 1);
editor.commit();
} else {
editor.putInt("tut", 0);
editor.commit();
}
}
});
AlertDialog.Builder welcomeDialog = new AlertDialog.Builder(this);
welcomeDialog.setTitle("Welcome");
welcomeDialog.setMessage("Welcome to Formula Calculator, if you want to know how to use my features " +
"click in the Tutorial button.");
welcomeDialog.setView(checkBoxView);
welcomeDialog.setCancelable(false);
welcomeDialog.setPositiveButton("Tutorial", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(tutorial);
}
});
welcomeDialog.setNegativeButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog welDialog = welcomeDialog.create();
welDialog.show();
}
非常感谢你的帮助!我无法找到应用程序关闭的解决方案。!
如果有帮助,这是logcat:
05-19 15:37:55.807: W/ActivityThread(8009): Application com.gabilheri.marcuscalculatorv2 can be
debugged on port 8100...
05-19 15:37:56.127: D/AndroidRuntime(8009): Shutting down VM
05-19 15:37:56.127: W/dalvikvm(8009): threadid=1: thread exiting with uncaught exception (group=0x40c32930)
05-19 15:37:56.137: E/AndroidRuntime(8009): FATAL EXCEPTION: main
05-19 15:37:56.137: E/AndroidRuntime(8009): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gabilheri.marcuscalculatorv2/com.gabilheri.marcuscalculatorv2.MainActivity}: java.lang.NullPointerException
05-19 15:37:56.137: E/AndroidRuntime(8009): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-19 15:37:56.137: E/AndroidRuntime(8009): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-19 15:37:56.137: E/AndroidRuntime(8009): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-19 15:37:56.137: E/AndroidRuntime(8009): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-19 15:37:56.137: E/AndroidRuntime(8009): at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 15:37:56.137: E/AndroidRuntime(8009): at android.os.Looper.loop(Looper.java:137)
05-19 15:37:56.137: E/AndroidRuntime(8009): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-19 15:37:56.137: E/AndroidRuntime(8009): at java.lang.reflect.Method.invokeNative(Native Method)
05-19 15:37:56.137: E/AndroidRuntime(8009): at java.lang.reflect.Method.invoke(Method.java:511)
05-19 15:37:56.137: E/AndroidRuntime(8009): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-19 15:37:56.137: E/AndroidRuntime(8009): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-19 15:37:56.137: E/AndroidRuntime(8009): at dalvik.system.NativeStart.main(Native Method)
05-19 15:37:56.137: E/AndroidRuntime(8009): Caused by: java.lang.NullPointerException
05-19 15:37:56.137: E/AndroidRuntime(8009): at com.gabilheri.marcuscalculatorv2.MainActivity.showAlertDialog(MainActivity.java:744)
05-19 15:37:56.137: E/AndroidRuntime(8009): at com.gabilheri.marcuscalculatorv2.MainActivity.onCreate(MainActivity.java:171)
05-19 15:37:56.137: E/AndroidRuntime(8009): at android.app.Activity.performCreate(Activity.java:5104)
05-19 15:37:56.137: E/AndroidRuntime(8009): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-19 15:37:56.137: E/AndroidRuntime(8009): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-19 15:37:56.137: E/AndroidRuntime(8009): ... 11 more
05-19 15:37:56.187: D/dalvikvm(8009): GC_CONCURRENT freed 219K, 5% free 7518K/7864K, paused 14ms+2ms, total 80ms
答案 0 :(得分:1)
如果tutCheck
CheckBox在Dialog Layout中,则将其初始化为:
View checkBoxView = View.inflate(this, R.layout.welcome_checkbox, null);
CheckBox welcomeCheck = (CheckBox)checkBoxView.findViewById(R.id.tutCheck);