我正在开发一个应用程序,其中我必须选择在自定义对话框中选择chkbox应用程序允许此但关闭对话框后再重新打开chkbox状态丢失并且全部取消选择 有人建议我使用共享首选项,这是我的代码: 图像允许用户打开对话框
ImageView img1 = (ImageView)findViewById(R.id.image_menu);
img1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.menu);
Button dialogButton = (Button) dialog.findViewById(R.id.btncross);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Button btnShare = (Button)dialog.findViewById(R.id.btnShare);
btnShare.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "Dry Day App ";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "DryDayApp");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
dialog.getContext().startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
});
cb1 = (CheckBox)dialog.findViewById(R.id.checkBoxHR);
cb1.setChecked(preferences.getBoolean("c1", false));
cb1.setOnCheckedChangeListener(listener1);
cb2 = (CheckBox)dialog.findViewById(R.id.checkBoxDay);
cb2.setChecked(preferences.getBoolean("c2", false));
cb2.setOnCheckedChangeListener(listener2);
cb3 = (CheckBox)dialog.findViewById(R.id.checkBox1);
cb3.setChecked(preferences.getBoolean("c3", false));
cb3.setOnCheckedChangeListener(listener3);
}
});
chkbox的监听器
private OnCheckedChangeListener listener1 = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if(isChecked){
cb1.setChecked(true);
cb2.setChecked(false);
cb3.setChecked(false);
preferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("c1", true);
editor.putBoolean("c2", false);
editor.putBoolean("c3", false);
editor.commit();
}
}
};
private OnCheckedChangeListener listener2 = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if(isChecked){
cb1.setChecked(false);
cb2.setChecked(true);
cb3.setChecked(false);
preferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("c1", false);
editor.putBoolean("c2", true);
editor.putBoolean("c3", false);
editor.commit();
}
}
};
private OnCheckedChangeListener listener3 = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if(isChecked){
cb1.setChecked(false);
cb2.setChecked(false);
cb3.setChecked(true);
//context.getSharedPreferences("YOUR_PREFS", 0).edit().clear().commit();
preferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("c1", false);
editor.putBoolean("c2", false);
editor.putBoolean("c3", true);
editor.commit();
}
}
};
now the application crashes showing null pointer exception
how to solve this issue
我的日志中的错误
05-21 13:14:44.420: E/AndroidRuntime(12169): FATAL EXCEPTION: main
05-21 13:14:44.420: E/AndroidRuntime(12169): java.lang.NullPointerException
05-21 13:14:44.420: E/AndroidRuntime(12169): at com.example.dd.MainActivity$7.onClick(MainActivity.java:262)
05-21 13:14:44.420: E/AndroidRuntime(12169): at android.view.View.performClick(View.java:4171)
05-21 13:14:44.420: E/AndroidRuntime(12169): at android.view.View$PerformClick.run(View.java:17195)
05-21 13:14:44.420: E/AndroidRuntime(12169): at android.os.Handler.handleCallback(Handler.java:643)
05-21 13:14:44.420: E/AndroidRuntime(12169): at android.os.Handler.dispatchMessage(Handler.java:92)
05-21 13:14:44.420: E/AndroidRuntime(12169): at android.os.Looper.loop(Looper.java:137)
05-21 13:14:44.420: E/AndroidRuntime(12169): at android.app.ActivityThread.main(ActivityThread.java:4803)
05-21 13:14:44.420: E/AndroidRuntime(12169): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 13:14:44.420: E/AndroidRuntime(12169): at java.lang.reflect.Method.invoke(Method.java:511)
05-21 13:14:44.420: E/AndroidRuntime(12169): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
05-21 13:14:44.420: E/AndroidRuntime(12169): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
05-21 13:14:44.420: E/AndroidRuntime(12169): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
试试这个..
prefernces = PreferenceManager.getDefaultSharedPreferences(this);
在onCreate()