Android:support.v4.app.Fragment 的解决方法 ->片段类转换异常?

发布于 2024-12-29 18:49:57 字数 717 浏览 1 评论 0原文

我正在尝试将 support.v4.app.Fragment 添加到 PreferenceActivity 标头,如下所示:

<header
    android:fragment="com.example.SupportFragmentSubClass"
    android:title="Selecting this should show the accompanying fragment" >
</header>

这会引发 ClassCastException,大概是因为 PreferenceActivity 期望有一个子 - android.app.Fragment 的类,而不是 support.v4.app.Fragment 的类。

我的用例是这样的:
我有非标准片段,我想在 <3.0 和 >3.0 设备上将其用作首选项。对于 >=3.0,我需要一个 android.app.Fragment 子类,以便它可以嵌入平板设备上首选项活动的“详细信息窗格”中。对于 <3.0,我需要一个 v4.support.app.Fragment 子类,这样我就可以在其中添加一个 ActivityFragment

有没有一种解决方法可以让我在这种情况下使用兼容性片段?

I'm trying to add a support.v4.app.Fragment to a PreferenceActivity header, like so:

<header
    android:fragment="com.example.SupportFragmentSubClass"
    android:title="Selecting this should show the accompanying fragment" >
</header>

This throws a ClassCastException, presumably because the PreferenceActivity is expecting a sub-class of android.app.Fragment, rather than support.v4.app.Fragment.

My use case is this:
I have non-standard Fragment that I want to use as a preference on both <3.0 and >3.0 devices. For >=3.0, I need an android.app.Fragment subclass so it can be embedded in the 'detail pane' of the preferences activity on tablet devices. For <3.0, I need a v4.support.app.Fragment subclass so I can throw in it an ActivityFragment.

Is there a workaround that would allow me to use a compatibility Fragment in this situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛放△進行李 2025-01-05 18:49:57

PreferenceFragment 不在 Android 支持包中,因此您不能以这种方式在 PreferenceActivity 中使用 Android 支持包 Fragment 类。此外,您的标头无论如何都无法在 Android 2.x 上运行,因为 Android 2.x 中的 PreferenceActivity 不了解片段。

原则上,您可以从源代码中派生 PreferenceActivity 来创建一个使用 Fragment Android 支持版本的活动。

或者,组织您的偏好以在 Android 3.0+ 上使用片段,并在 Android 2.x 上避免使用它们。 这是一个示例项目,我在其中演示了一种执行此操作的方法。

PreferenceFragment is not in the Android Support package, and you cannot use an Android Support package Fragment class in a PreferenceActivity this way. Moreover, your headers would not work on Android 2.x anyway, since the PreferenceActivity in Android 2.x does not know about fragments.

In principle, you could fork the PreferenceActivity from the source code to create one that does use the Android Support version of Fragment.

Or, organize your preferences to use fragments on Android 3.0+ and avoid them on Android 2.x. Here is a sample project where I demonstrate a way to do this.

魔法唧唧 2025-01-05 18:49:57

正如@CommonsWare 指出的那样,如果不重写 PreferenceActivity 就不可能达到我想要的效果,而且这看起来是一项繁重的工作。

我选择的不太优雅的解决方案是创建两个 PreferenceActivities (as此处所示)并创建两个 Fragment 子类,每个子类对应一种 Fragment 风格。

因此,PrefsActivityHC 添加了此标头:

<header
    <!-- An android.app.Fragment subclass -->
    android:fragment="com.example.project.MyFragmentHC"
</header>

...而 PrefsActivity 添加了此首选项:

<Preference>
    <intent
        <!-- A v4.support.app.Fragment subclass, wrapped in an ActivityFragment -->
        android:targetClass="com.example.project.MyFragmentActivity"
        android:targetPackage="com.example.project" >
    </intent>
</Preference>

为了最大限度地减少两个几乎相同的片段所需的代码重复量,我创建了一个 < code>MyFragmentDelegate 类,支持常见的片段方法,并在 MyFragmentMyFragmentHC 中保存该类的实例。然后,对这些片段中的方法的调用将转发给委托:

class MyFragment {

    MyFragmentDelegate mDelegate;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return mDelegate.onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    }
}

class MyFragmentHC {

    MyFragmentDelegate mDelegate;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return mDelegate.onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    }
}

As @CommonsWare points out, it's not possible to what I wanted without rewriting PreferenceActivity, and that looks like a load of work.

The not-so-elegant solution I settled on was to create two PreferenceActivities (as shown here) and also create two Fragment subclasses, one for each flavour of Fragment.

So, PrefsActivityHC adds this header:

<header
    <!-- An android.app.Fragment subclass -->
    android:fragment="com.example.project.MyFragmentHC"
</header>

...while PrefsActivity adds this preference:

<Preference>
    <intent
        <!-- A v4.support.app.Fragment subclass, wrapped in an ActivityFragment -->
        android:targetClass="com.example.project.MyFragmentActivity"
        android:targetPackage="com.example.project" >
    </intent>
</Preference>

To minimise the amount of code duplication required to have two near-identical fragments, I created a MyFragmentDelegate class that supports common fragment methods, and held an instance of that in MyFragment and MyFragmentHC. Calls to the methods in these fragments are then just forwarded to the delegate:

class MyFragment {

    MyFragmentDelegate mDelegate;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return mDelegate.onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    }
}

class MyFragmentHC {

    MyFragmentDelegate mDelegate;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return mDelegate.onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文