使用兼容性库的 ListFragment 子类上的 ClassCastException
只有在使用 3.0 之前的设备的兼容性库时才会发生这种情况
我收到无法确定的错误。我有一个带有 ListFragment 和标准片段的活动。它就像 Android 开发指南的开发人员部分中提供的示例一样。
ListFragment 子类(没有重写函数)
public class ItemListFragment extends ListFragment
MainActivity
public class ItemViewerActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_viewer);
}
}
MainActivity 的 Xml 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<fragment class="org.example.ItemListFragment"
android:id="@+id/item_list_fragment"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/item_info_frame"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
来自 LogCat 的错误消息
ERROR/AndroidRuntime:由以下原因引起: java.lang.ClassCastException: org.example.ItemListFragment 无法转换为 android.app.Fragment
This only happens when using the compatibility library for pre-3.0 devices
I'm getting an error that I cannot pin down. I have an Activity with a ListFragment and standard Fragment. It is just like the example provided in the Developers section of the Android Dev Guide.
ListFragment Subclass (no functions overridden)
public class ItemListFragment extends ListFragment
MainActivity
public class ItemViewerActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_viewer);
}
}
Xml Layout for MainActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<fragment class="org.example.ItemListFragment"
android:id="@+id/item_list_fragment"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/item_info_frame"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
Error Message from LogCat
ERROR/AndroidRuntime: Caused by: java.lang.ClassCastException: org.example.ItemListFragment cannot be cast to android.app.Fragment
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过一番认真的谷歌搜索后,我发现了一篇文章,指出了一个不错的小花絮。使用兼容性库时,使用片段的活动必须扩展 FragmentActivity。一旦我这样做了,错误就不再出现了。
After some serious googling, I found an article that pointed out a nice little tidbit. When using the compatibility library, your activities that use fragments have to extend FragmentActivity. Once I did that, the error did not present itself again.
对我来说,问题是我忘记更改清单。按照教程的建议,我将 Activity 转换为 Fragment 并创建了一个 shell Activity 来调用它。我的清单仍然指向导致类转换异常的 Fragment 类。
For me the problem was that I had forgotten to change the manifest. Following the suggestion of the tutorial I converted my Activity to a Fragment and made a shell Activity to call it. My manifest still pointed to the Fragment class leading to a Class Cast Exception.