android应用程序中出现错误:尝试在空对象引用上调用接口方法
我在radiogroup和want中有三个单选按钮,当用户选中任何单选按钮时,它必须保持选中状态,直到用户选中任何其他按钮(无论用户是否关闭应用程序),为此,我使用SharedReferences存储此选中的复选框值,并在加载活动时检索,但当我加载用于选择复选框的活动时出错。下面是我的代码 Activity.java:android应用程序中出现错误:尝试在空对象引用上调用接口方法,android,nullpointerexception,sharedpreferences,android-radiogroup,android-radiobutton,Android,Nullpointerexception,Sharedpreferences,Android Radiogroup,Android Radiobutton,我在radiogroup和want中有三个单选按钮,当用户选中任何单选按钮时,它必须保持选中状态,直到用户选中任何其他按钮(无论用户是否关闭应用程序),为此,我使用SharedReferences存储此选中的复选框值,并在加载活动时检索,但当我加载用于选择复选框的活动时出错。下面是我的代码 Activity.java: public static SharedPreferences pref; public static String lang; public static String chk
public static SharedPreferences pref;
public static String lang;
public static String chk;
public static RadioButton gujarati;
public static RadioButton hindi;
public static RadioButton english;
public static EditText txtV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_language);
//
// String chk = pref.getString("key_name", null);
// if(chk=="Gujarati"){
// gujarati.setChecked(true);
// }
// else if(chk=="Hindi"){
// hindi.setChecked(true);
// }
// else{
// english.setChecked(true);
//}
//editor.remove("key_name3"); // will delete key key_name3
//editor.remove("key_name4"); // will delete key key_name4
// Save the changes in SharedPreferences
//editor.commit(); // commit changes
//editor.clear();
//editor.commit();
RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1);
gujarati = (RadioButton) findViewById(R.id.radio0);
hindi = (RadioButton) findViewById(R.id.radio1);
english = (RadioButton) findViewById(R.id.radio2);
txtV = (EditText) findViewById(R.id.editText1);
//int radio_selected = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE).getInt("my_selected_radio",0);
//int radio_selected = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE).edit().putInt("my_selected_radio",selectedValue).commit();
chk = pref.getString("key_name", null);
if(chk == "Gujarati"){
gujarati.setChecked(true);
}
if(chk == "Hindi"){
hindi.setChecked(true);
}
if(chk == "English"){
english.setChecked(true);
}
group.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId == R.id.radio0) {
lang = "Gujarati";
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
editor.putString("key_name", lang);
editor.commit();
chk = pref.getString("key_name", null);
txtV.setText(chk);
} else if(checkedId == R.id.radio1) {
lang = "Hindi";
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
editor.putString("key_name", lang);
editor.commit();
chk = pref.getString("key_name", null);
txtV.setText(chk);
} else {
lang = "English";
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
editor.putString("key_name", lang);
editor.commit();
chk = pref.getString("key_name", null);
txtV.setText(chk);
}
}
});
activity.xml:
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="25dp" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup1"
android:layout_below="@+id/radioGroup1"
android:layout_marginLeft="15dp"
android:ems="10" >
<requestFocus />
</EditText>
您得到了NPE,因为在
chk=pref.getStringkey\u名称,空
在使用前初始化pref。喜欢
pref = getApplicationContext().getSharedPreferences("MyPref", 0); in onCreate(....)
在这里:
chk=pref.getStringkey\u名称,空
一,。您不是在初始化SharedReferences的pref实例,而是在调用getString方法。按如下方式操作:
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
chk = pref.getString("key_name", null);
二,。使用String.equals比较字符串,而不是==
三,。在onCreate方法中初始化pref,则无需像当前在setOnCheckedChangeListener中所做的那样再次初始化,因为您已经实现了onCheckedChangeListener事件,每当活动启动时,该事件总是自动调用。因此,出现的问题是,虽然您的SharedReference值是正确的,但之后会立即调用CheckedChangeListener,因此您的首选项中的值会被重置,并且您会得到错误的结果 这就是为什么每当您要检查另一个单选按钮时,我都会删除旧值
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_language);
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor1 = pref.edit();
editor1.remove("key_name");
if (checkedId == R.id.radio0) {
lang = "Gujarati";
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
editor.putString("key_name", lang);
editor.commit();
// chk = pref.getString("key_name", null);
txtV.setText(lang);
} else if (checkedId == R.id.radio1) {
lang = "Hindi";
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
editor.putString("key_name", lang);
editor.commit();
// chk = pref.getString("key_name", null);
txtV.setText(lang);
} else if (checkedId == R.id.radio2) {
lang = "English";
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
editor.putString("key_name", lang);
editor.commit();
// chk = pref.getString("key_name", null);
txtV.setText(lang);
}
}
});
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
chk = pref.getString("key_name", null);
System.err.println("Get value is=====" + chk);
if (chk != null) {
if (chk.equals("Gujarati")) {
gujarati.setChecked(true);
}
if (chk.equals("Hindi")) {
hindi.setChecked(true);
}
if (chk.equals("English")) {
english.setChecked(true);
}
}
}
请同时发布您的logcat错误。如果要比较字符串变量值,则ifchk==Gujarati无效。我已添加了您的代码,但当我重新启动时,应用程序仍会忘记选中单选按钮的所有状态。我认为在将所选复选框值存储到SharedReference中后,您无需再次从中获取它来设置它当您在setOnCheckedChangeListener方法中的字符串中有每个选定按钮的值时,将其输入到TextView中。您只能直接使用该字符串值。我已经尝试了您的代码,但在重新启动我的应用程序后,它再次忘记了复选框,请帮助我。@RaviBhalodiya现在尝试我的代码,我已经更新了它。。现在工作正常了。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_language);
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor1 = pref.edit();
editor1.remove("key_name");
if (checkedId == R.id.radio0) {
lang = "Gujarati";
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
editor.putString("key_name", lang);
editor.commit();
// chk = pref.getString("key_name", null);
txtV.setText(lang);
} else if (checkedId == R.id.radio1) {
lang = "Hindi";
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
editor.putString("key_name", lang);
editor.commit();
// chk = pref.getString("key_name", null);
txtV.setText(lang);
} else if (checkedId == R.id.radio2) {
lang = "English";
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
editor.putString("key_name", lang);
editor.commit();
// chk = pref.getString("key_name", null);
txtV.setText(lang);
}
}
});
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
chk = pref.getString("key_name", null);
System.err.println("Get value is=====" + chk);
if (chk != null) {
if (chk.equals("Gujarati")) {
gujarati.setChecked(true);
}
if (chk.equals("Hindi")) {
hindi.setChecked(true);
}
if (chk.equals("English")) {
english.setChecked(true);
}
}
}