开发者

ClassCastException on a subclass of ListFragment using compatibility library

开发者 https://www.devze.com 2023-03-02 05:59 出处:网络
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

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消