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

Posted

技术标签:

【中文标题】在我的“设置”片段中膨胀类 PreferenceScreen 时出错【英文标题】:Error inflating class PreferenceScreen inside my 'settings' fragment 【发布时间】:2020-08-09 06:17:18 【问题描述】:

我一直试图让这段代码工作一段时间,我按照教程将使用片段的底部导航视图集成到我的应用程序中。这工作得很好,但是一旦我为我的设置片段设置了一个首选项屏幕,应用程序就会在选择它时立即崩溃。 我相信我可能误解了如何扩大偏好屏幕,但不知道如何去做,因为某些方法已被弃用。

这里是 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 Activity 文件如下所示:

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) 

非常感谢您的帮助,谢谢!

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

非常感谢,但现在我还有另一个问题,设置与我的计步器重叠,这是应用程序启动的活动。有什么办法可以抵消吗? 通常你如何使用片段是在你的布局中有一个FrameLayout。您将在片段中拥有初始布局,然后使用 replace()addToBackStack() 将初始布局替换为新片段。听起来您没有在活动的其他地方使用片段。您可以考虑使用migrating to Navigation,它为您提供了从活动转移到基于片段的结构的分步说明。 是的,你是对的,最初的计步器布局不在片段内,我已经相应地调整了它。感谢您的所有帮助!

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

片段没有被 RecyclerView 膨胀

第 7 行的 InflateException 二进制 XML 文件:膨胀类片段时出错

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

如何在另一个片段内膨胀另一个布局

android.view.InflateException:二进制 XML 文件:膨胀类片段时出错

膨胀类 android.support.v7.app.AlertController.RecycleListView 时出错