Android:support.v4.app.Fragment 的解决方法 ->片段类转换异常?
我正在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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 aPreferenceActivity
this way. Moreover, your headers would not work on Android 2.x anyway, since thePreferenceActivity
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 ofFragment
.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.
正如@CommonsWare 指出的那样,如果不重写 PreferenceActivity 就不可能达到我想要的效果,而且这看起来是一项繁重的工作。
我选择的不太优雅的解决方案是创建两个 PreferenceActivities (as此处所示)并创建两个 Fragment 子类,每个子类对应一种 Fragment 风格。
因此,
PrefsActivityHC
添加了此标头:...而
PrefsActivity
添加了此首选项:为了最大限度地减少两个几乎相同的片段所需的代码重复量,我创建了一个 < code>MyFragmentDelegate 类,支持常见的片段方法,并在
MyFragment
和MyFragmentHC
中保存该类的实例。然后,对这些片段中的方法的调用将转发给委托: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:...while
PrefsActivity
adds this 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 inMyFragment
andMyFragmentHC
. Calls to the methods in these fragments are then just forwarded to the delegate: