【发布时间】:2021-01-21 08:24:27
【问题描述】:
在应用程序中有两个单选按钮,每个按钮都有自己的值,第一个单选按钮的值为“2”,第二个单选按钮的值为“4”。现在我成功实现了共享首选项,以便通过单击单选按钮保存值。但是缺少一件事,如果我选择单选按钮 1,并且如果我关闭应用程序并再次打开应用程序,那么单选按钮 1 未被选中。我还尝试添加radiobutton.setChecked,但它会崩溃。下面是xml和java代码。
custom_dialog.xml
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioGroup" />
</ScrollView>
method0.xml
<resources>
<integer-array name="method0">
<item>2</item>
</integer-array>
</resources>
method1.xml
<resources>
<integer-array name="method1">
<item>4</item>
</integer-array>
</resources>
CustomDialog.java
public class CustomDialog extends AppCompatDialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
LayoutInflater layoutInflater=getActivity().getLayoutInflater();
final View view=layoutInflater.inflate(R.layout.custom_dialog,null);
RadioGroup radioGroup=view.findViewById(R.id.radioGroup);
final int[] num0=getResources().getIntArray(R.array.method0);
final int[] num1=getResources().getIntArray(R.array.method1);
RadioButton radioButton0=new RadioButton(getActivity());
RadioButton radioButton1=new RadioButton(getActivity());
radioButton0.setText("A");
radioButton0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int method=num0[0];
SharedPreferences sharedPreferences=getActivity().getSharedPreferences("METHOD",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putInt("key", method);
editor.apply();
}
});
radioButton0.setPadding(0,50,0,50);
radioButton0.setTextSize(17);
radioButton0.setTypeface(null, Typeface.BOLD);
radioButton0.setChecked(Update("key")); //Here this line it is getting crashed
radioButton1.setText("B");
radioButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int method=num1[0];
SharedPreferences sharedPreferences=getActivity().getSharedPreferences("METHOD",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putInt("key", method);
editor.apply();
}
});
radioButton1.setPadding(0,50,0,50);
radioButton1.setTextSize(17);
radioButton1.setTypeface(null, Typeface.BOLD);
radioGroup.addView(radioButton0);
radioGroup.addView(radioButton1);
builder.setView(view)
.setTitle("Select Any Methods")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
private boolean Update(String key){
SharedPreferences sharedPreferences=getActivity().getSharedPreferences("METHOD",Context.MODE_PRIVATE);
return sharedPreferences.getBoolean(key,false); //Here this line it is getting crashed
}
}
MainActivity.java
btnMethod.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialog(); //this will open the dialog along with two radiobuttons
}
});
private void openDialog() {
CustomDialog customDialog=new CustomDialog();
customDialog.show(getSupportFragmentManager(),"Custom Dialog");
}
Logcat
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nabil.myapplication, PID: 17062
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Boolean
at android.app.SharedPreferencesImpl.getBoolean(SharedPreferencesImpl.java:326)
at com.nabil.myapplication.CustomDialog.Update(CustomDialog.java:295)
at com.nabil.myapplication.CustomDialog.onCreateDialog(CustomDialog.java:69)
【问题讨论】:
标签: java android android-studio radio-button sharedpreferences