链接首选项屏幕

问题描述 投票:0回答:1

tl/dr:我无法将首选项屏幕链接到另一个首选项屏幕。我收到运行时错误。

场景:我有一个正在运行的 Xamarin/Android 应用程序,它使用 AndroidX.Preference 类来使用单个首选项 xml 资源和片段来实现首选项活动。这工作正常。我想将一些更高级的首选项拆分为单独的首选项屏幕,如此处所述,以提高可用性。

问题:应用程序从 nuget 导入 Xamarin.AndroidX.Preference v1.2.1.2。实现了链接文章(上面)中描述的方法以及调用首选项活动的代码后,当我运行它时,链接(“根”)首选项屏幕将按预期显示。当我单击链接首选项时,Android 无法实例化链接首选项屏幕的片段,并抛出

AndroidX.Fragment.App.Fragment.InstantiationException
异常,并显示消息“
Unable to instantiate fragment com.companyname.app1.AdvancedPreferencesFragment: make sure class name exists
”。我不明白为什么会发生这种情况,因为这个类确实存在。

示例代码:我创建了一个简单的示例应用程序,它展示了我所看到的问题,并且尽可能简洁。继承人 PreferencesActivity.cs:

namespace App1
{
    [Android.App.Activity(Label = "PreferencesActivity")]
    public class PreferencesActivity : AndroidX.AppCompat.App.AppCompatActivity//, AndroidX.Preference.PreferenceFragmentCompat.IOnPreferenceStartFragmentCallback
    {
        protected override void OnCreate(Android.OS.Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.preferences_activity);
            SupportFragmentManager.BeginTransaction().Replace(Resource.Id.preferences_fragment_container, new RootPreferencesFragment()).Commit();
        }
    }
}

这是它的preference_activity.xml布局资源:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/preferences_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

这实例化了

RootPreferencesFragment
,即:

namespace App1
{
    public class RootPreferencesFragment : AndroidX.Preference.PreferenceFragmentCompat
    {
        public override void OnCreate(Android.OS.Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
        }

        public override void OnCreatePreferences(Android.OS.Bundle savedInstanceState, string rootKey)
        {
            SetPreferencesFromResource(Resource.Xml.preferences_root, rootKey);
        }
    }
}

及其对应的xml资源,

preferences_root.xml
:

<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.preference.SwitchPreference
        android:key="pref1"
        android:title="Prefercence 1"/>
    <androidx.preference.Preference
        android:title="Advanced"
        app:fragment="com.companyname.app1.AdvancedPreferencesFragment"/>
</androidx.preference.PreferenceScreen>

上面的 linking

<Preference>
元素指定了一个名为
com.companyname.app1.AdvancedPreferencesFragment
的片段,它是 linked 首选项屏幕片段的全名:

namespace App1
{
    public class AdvancedPreferencesFragment : AndroidX.Preference.PreferenceFragmentCompat
    {
        public override void OnCreate(Android.OS.Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
        }

        public override void OnCreatePreferences(Android.OS.Bundle savedInstanceState, string rootKey)
        {
            SetPreferencesFromResource(Resource.Xml.preferences_advanced, rootKey);
        }
    }
}

及其对应的xml资源,

preferences_advanced.xml
:

<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.preference.SwitchPreference
        android:key="pref2"
        android:title="Advanced Prefercence 2"/>
</androidx.preference.PreferenceScreen>

我尝试过的

我认为问题在于链接首选项元素的

app:fragment
属性的值,该属性用于实例化链接片段。该应用程序的包名称是
com.companyname.app1
,C# 命名空间是
App1
,因此我尝试了此属性的以下值:

  • com.companyname.App1.AdvancedPreferencesFragment
  • com.companyname.app1.AdvancedPreferencesFragment
  • com.companyname.app1.App1.AdvancedPreferencesFragment
  • com.companyname.app1.app1.AdvancedPreferencesFragment
  • App1.AdvancedPreferencesFragment
  • app1.AdvancedPreferencesFragment
  • AdvancedPreferencesFragment

它们都不起作用,并且都给了我相同的异常,与属性值匹配的消息文本略有不同。

我还尝试向两个片段类添加空的默认构造函数。

Windows 11 x64 上的 VS 2022 x64 v17.7.5 和 Xamarin 17.7.0.223。

对上面的代码量表示歉意,但我没有想法。有人可以告诉我我做错了什么吗?谢谢。

android xamarin android-fragments xamarin.android
1个回答
0
投票

问题在于 Xamarin. AndroidX。 Preference v1.2.1.2 NuGet 包与最新版本的 AndroidX Fragment 库不兼容。要解决该问题,您需要升级 Xamarin. AndroidX。优选 NuGet 包至少为 v1.3.0.2。

升级 NuGet 包后,您将需要重建您的应用程序。重建应用程序后,您应该能够实例化链接的首选项屏幕的片段,没有任何问题。

以下是升级 Xamarin.AndroidX.Preference NuGet 包的步骤:

在 Visual Studio 中打开 NuGet 包管理器。 搜索 Xamarin.AndroidX.Preference NuGet 包。 选择 Xamarin.AndroidX。首选项 v1.3.0.2 NuGet 包并单击“安装”按钮。 单击“确定”按钮确认安装。 安装 NuGet 包后,您将需要重建您的应用程序。要重建您的应用程序,请单击“构建”>“重建解决方案”菜单项。

应用程序重建后,您应该能够实例化链接的首选项屏幕的片段,没有任何问题。

© www.soinside.com 2019 - 2024. All rights reserved.