1

我对 Android Studio 中的 PreferenceScreen 有疑问。我使用 MultiSelectListPreference 并没有收到错误,但是当我使用 ListPreference 时,我收到错误并且没有打开设置活动。

<PreferenceCategory
    android:title="Time Table Setting">

    <ListPreference
        android:key="Header_List"
        android:entries="@array/setting_header_list"
        android:entryValues="@array/setting_header_values"
        android:title="Display Dates"
        android:summary="Monday to Friday, Saturday or Sunday"/>
</PreferenceCategory>

<Preference
    android:key="Restart"
    android:title="Save"/>

如果您有任何建议,请帮助我。谢谢

这是我收到的错误消息

2020-03-21 22:06:26.212 14206-14206/com.pythontaro.personaltimetable E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.name, PID: 14206
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.name.appname/com.name.appname.setting.SettingsActivity}: java.lang.ClassCastException: java.util.HashSet cannot be cast to java.lang.String
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
 Caused by: java.lang.ClassCastException: java.util.HashSet cannot be cast to java.lang.String
    at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:225)
    at androidx.preference.Preference.getPersistedString(Preference.java:1686)
    at androidx.preference.ListPreference.onSetInitialValue(ListPreference.java:255)
    at androidx.preference.Preference.onSetInitialValue(Preference.java:1614)
    at androidx.preference.Preference.dispatchSetInitialValue(Preference.java:1587)
    at androidx.preference.Preference.onAttachedToHierarchy(Preference.java:1311)
    at androidx.preference.Preference.onAttachedToHierarchy(Preference.java:1326)
    at androidx.preference.PreferenceGroup.addPreference(PreferenceGroup.java:249)
    at androidx.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:170)
    at androidx.preference.PreferenceInflater.rInflate(PreferenceInflater.java:345)
    at androidx.preference.PreferenceInflater.rInflate(PreferenceInflater.java:346)
    at androidx.preference.PreferenceInflater.inflate(PreferenceInflater.java:157)
    at androidx.preference.PreferenceInflater.inflate(PreferenceInflater.java:109)
    at androidx.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:216)
    at androidx.preference.PreferenceFragmentCompat.addPreferencesFromResource(PreferenceFragmentCompat.java:361)
    at com.name.appname.setting.SettingsActivity$SettingsFragment.onCreatePreferences(SettingsActivity.java:28)
    at androidx.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:160)
    at androidx.fragment.app.Fragment.performCreate(Fragment.java:2684)
    at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1175)
    at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1255)
    at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1138)
    at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:136)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1989)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
    at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2629)
    at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577)
    at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:247)
    at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541)
    at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
    at android.app.Activity.performStart(Activity.java:6679)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

这是设置活动类中的代码

public class SettingsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_activity);
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.settings, new SettingsFragment())
            .commit();
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

public static class SettingsFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.settings);
    }
}}

这是字符串值的代码

 <!--Setting-->
<string-array name="setting_header_list">
    <item>"Friday(default)"</item>
    <item>"Saturday"</item>
    <item>"Sunday"</item>
</string-array>

<string-array name="setting_header_values">
    <item>0</item>
    <item>1</item>
    <item>2</item>
</string-array>

这是 SharedPreferencesImpl.java 中第 225 行的代码

final StructStat stat;
    try {
        /*
         * Metadata operations don't usually count as a block guard
         * violation, but we explicitly want this one.
         */
        BlockGuard.getThreadPolicy().onReadFromDisk();
        stat = Os.stat(mFile.getPath());
    } catch (ErrnoException e) {
        return true;
    }
4

1 回答 1

0

从您的堆栈跟踪来看,这是关键:

Caused by: java.lang.ClassCastException: java.util.HashSet cannot be cast to java.lang.String

您正在尝试使用 a 正在使用的java.util.HashSet地方java.lang.String

它在SharedPreferencesImpl.java:225getString被调用时。如果您不共享代码的相关部分,这就是我所能做到的。

编辑

正如提问者在评论部分中指出的那样,问题在于字符串数组项上使用了双引号。

于 2020-03-22T02:28:59.320 回答