【问题标题】:java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Boolean Shared Preference (RadioButtons)java.lang.ClassCastException:java.lang.Integer 不能转换为 java.lang.Boolean Shared Preference (RadioButtons)
【发布时间】: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


    【解决方案1】:

    我对android代码不太了解,但是,如果我只是通过你的错误,那么它就来了,因为Integer和boolean这两个类之间没有父子关系。

    【讨论】:

    • 是的,我将密钥存储在int 中的共享首选项中,当我在boolean 中调用此函数radiobutton.setChecked 时,它会崩溃。
    【解决方案2】:

    这是因为您在共享首选项中存储了一个整数,然后尝试检索一个布尔值。

    我建议像这样存储单选按钮的值:

    editor.putBoolean("radioButton0", radioButton0.isChecked());
    
    ...
    
    editor.putBoolean("radioButton1", radioButton1.isChecked());
    

    已编辑:

    而不是使用

    editor.putInt("key", method);
    

    用途:

    editor.putBoolean("radioButton0", radioButton0.isChecked());
    

    而不是使用

    radioButton0.setChecked(Update("key"));
    

    用途:

    radioButton0.setChecked(Update("radioButton0"));
    

    这应该像你想要的那样工作。

    【讨论】:

    • 我使用editor.putInt 来存储值并传递给api,我成功地将这个存储int 值传递给api。但是当谈到radiobutton.setChecked 时,如果关闭应用程序并重新打开该选定的单选按钮未选中并且也会崩溃。
    • @SOUGATRIYADH 将值存储为布尔值并检索为布尔值,或者存储为 Int 并检索为 Int。您不应保存为 int 并检索为 boolean。
    • @iamanbansal 很好,我可以检索 int 值,但例如我选择第一个单选按钮并将int 值存储在shared preference 现在我决定关闭应用程序并再次我打开应用程序,我在关闭应用程序之前选择的第一个单选按钮仍未选中。
    • 检查编辑的答案,也许我想说的更清楚
    猜你喜欢
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    相关资源
    最近更新 更多