gpt4 book ai didi

java - 在 PreferenceManager.setDefaultValues() 上获取 ClassCastException

转载 作者:行者123 更新时间:2023-11-30 05:38:58 33 4
gpt4 key购买 nike

我收到此异常

Caused by: java.lang.ClassCastException: androidx.preference.PreferenceScreen cannot be cast to android.preference.GenericInflater$Parent

关于执行PreferenceManager.setDefaultValues(this, R.xml.root_preferences, false);
在主要 Activity 中。

根据this因此,如果 list 文件中的 Activity 及其父 Activity 相同,则可能会发生上述错误,但我的情况并非如此。我只是不明白为什么会抛出这个异常。

AndroidManifest.xml

         <activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.android.droidcafeinput.MainActivity" />
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

root_preferences.xml

<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">

<PreferenceCategory app:title="@string/messages_header">

<EditTextPreference
app:key="signature"
app:title="@string/signature_title"
app:useSimpleSummaryProvider="true" />

<ListPreference
app:defaultValue="reply"
app:entries="@array/reply_entries"
app:entryValues="@array/reply_values"
app:key="reply"
app:title="@string/reply_title"
app:useSimpleSummaryProvider="true" />

</PreferenceCategory>

<PreferenceCategory app:title="@string/sync_header">

<SwitchPreferenceCompat
app:key="sync"
app:title="@string/sync_title" />

<SwitchPreferenceCompat
app:dependency="sync"
app:key="attachment"
app:summaryOff="@string/attachment_summary_off"
app:summaryOn="@string/attachment_summary_on"
app:title="@string/attachment_title" />

</PreferenceCategory>

</androidx.preference.PreferenceScreen>

SettingsActivity.java

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) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
}

堆栈跟踪

Caused by: Java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.droidcafeinput/com.example.android.droidcafeinput.MainActivity}: java.lang.ClassCastException: androidx.preference.PreferenceScreen cannot be cast to android.preference.GenericInflater$Parent
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.ClassCastException: androidx.preference.PreferenceScreen cannot be cast to android.preference.GenericInflater$Parent
at android.preference.GenericInflater.inflate(GenericInflater.java:321)
at android.preference.GenericInflater.inflate(GenericInflater.java:264)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:324)
at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:650)
at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:609)
at com.example.android.droidcafeinput.MainActivity.onCreate(MainActivity.java:74)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

最佳答案

看起来您正在尝试将 android.preference.PreferenceManager 与 AndroidX 库一起使用。这些库不兼容:由于您在其他地方使用 AndroidX 库,因此请确保没有任何 android.preference 导入。此外,在 AndroidX 库中,您不需要调用 setDefaultValues,因为默认值会自动读取和使用 - 如果您只需删除该行,它应该可以工作。

关于java - 在 PreferenceManager.setDefaultValues() 上获取 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56076624/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com