在“设置”片段中夸大类PreferenceScreen的错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在“设置”片段中夸大类PreferenceScreen的错误相关的知识,希望对你有一定的参考价值。

我一直在尝试使这段代码运行一段时间,我按照一个教程将使用片段的底部导航视图集成到我的应用程序中。效果很好,但是一旦我为我的设置片段设置了首选项屏幕,应用程序在选择它时立即崩溃。 我相信我可能误解了如何使首选项屏幕膨胀,但由于某些方法已被弃用,因此不知道如何进行此操作。

这是res / xml / fragment_settings.xml

<PreferenceCategory
    android:title="Category 1">

    <SwitchPreferenceCompat
        android:key="key1"
        android:title="Switch Preference"
        android:summary="Switch Summary"
        android:defaultValue="true" />

    <EditTextPreference
        android:key="key2"
        android:title="EditText Preference"
        android:summary="EditText Summary"
        android:dialogMessage="Dialog Message"
        android:defaultValue="Default value" />
    <CheckBoxPreference
        android:key="key3"
        android:title="CheckBox Preference"
        android:summary="CheckBox Summary"
        android:defaultValue="true"/>
</PreferenceCategory>

这是SettingsFragment Java活动文件的外观:

package com.example.managinghealthapplicationv1;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.preference.PreferenceManager;

public class SettingsFragment extends Fragment {

    @SuppressLint("ResourceType")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.xml.fragment_settings, container, false);

    }
}

这是我收到的错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.managinghealthapplicationv1, PID: 11016
android.view.InflateException: Binary XML file line #1 in com.example.managinghealthapplicationv1:xml/fragment_settings: Binary XML file line #1 in com.example.managinghealthapplicationv1:xml/fragment_settings: Error inflating class PreferenceScreen
Caused by: android.view.InflateException: Binary XML file line #1 in com.example.managinghealthapplicationv1:xml/fragment_settings: Error inflating class PreferenceScreen
Caused by: java.lang.ClassNotFoundException: android.view.PreferenceScreen
    at java.lang.Class.classForName(Native Method)
    at java.lang.Class.forName(Class.java:454)
    at android.view.LayoutInflater.createView(LayoutInflater.java:815)
    at android.view.LayoutInflater.createView(LayoutInflater.java:776)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:913)
    at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:930)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:950)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:659)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
    at com.example.managinghealthapplicationv1.SettingsFragment.onCreateView(SettingsFragment.java:20)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
    at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
    at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447)
    at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2169)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1992)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
    at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.PreferenceScreen" on path: DexPathList[[zip file "/data/app/com.example.managinghealthapplicationv1-5rs7Qpv6NmVeBQNlRb7Frg==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.managinghealthapplicationv1-5rs7Qpv6NmVeBQNlRb7Frg==/lib/x86, /system/lib, /system/product/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at java.lang.Class.classForName(Native Method) 
    at java.lang.Class.forName(Class.java:454) 
    at android.view.LayoutInflater.createView(LayoutInflater.java:815) 
    at android.view.LayoutInflater.createView(LayoutInflater.java:776) 
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:913) 
    at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68) 
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:930) 
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:950) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:659) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:534) 
    at com.example.managinghealthapplicationv1.SettingsFragment.onCreateView(SettingsFragment.java:20) 
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698) 
    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320) 
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187) 
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356) 
    at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434) 
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497) 
    at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447) 
    at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2169) 
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1992) 
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947) 
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849) 
    at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413) 
    at android.os.Handler.handleCallback(Handler.java:883) 
    at android.os.Handler.dispatchMessage(Handler.java:100) 
    at android.os.Looper.loop(Looper.java:214) 
    at android.app.ActivityThread.main(ActivityThread.java:7356) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

非常感谢您,谢谢!

答案

根据Settings documentation,您需要使用PreferenceFragmentCompatPreferences library中的setPreferencesFromResource()

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

您需要在代码中添加@SuppressLint("ResourceType")的原因是,首选项XML文件不是布局XML文件。因此,为什么它不适用于onCreateView()以及为什么setPreferencesFromResource存在。

以上是关于在“设置”片段中夸大类PreferenceScreen的错误的主要内容,如果未能解决你的问题,请参考以下文章

在我的“设置”片段中膨胀类 PreferenceScreen 时出错

错误夸大类com.google.android.material.bottomnavigation.BottomNavigationView(材料设计)

android:来自片段的菜单项单击事件

如何设置画布大小?

Android Navigation java.language.IllegalStateException 片段类未设置

使用带有片段的部分,NullPointerException