Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException on a subclass of ListFragment using compatibility library

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

like image 303
Spidy Avatar asked May 03 '11 19:05

Spidy


People also ask

What is a ClassCastException?

It is a runtime exception that occurs when the application code attempts to cast an object to another class of which the original object is not an instance. For example, a String object cannot be cast to an Integer object and attempting to do so will result in a ClassCastException.

What are class cast exceptions in Java?

- GeeksforGeeks How to Solve Class Cast Exceptions in Java? An unexcepted, unwanted event that disturbed the normal flow of a program is called Exception. Most of the time exceptions are caused by our program and these are recoverable. Example: If our program requirement is to read data from the remote file locating at the U.S.A.

Why do we get exceptions when casting between two classes?

When we try to cast an object of Parent class to its Child class type, this exception will be thrown. When we try to cast an object of one class into another class type that has not extended the other class or they don't have any relationship between them.

Should ClassCastException be declared in throws clause?

Since the ClassCastException is an unchecked exception, it doesn't need to be declared in the throws clause of a method or constructor. Some of the most common sources of ClassCastException in Java are: Using collections like HashMap, ArrayList and HashTable which do not use Java Generics.


2 Answers

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.

like image 154
Spidy Avatar answered Oct 25 '22 14:10

Spidy


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.

like image 32
pierre renoir Avatar answered Oct 25 '22 14:10

pierre renoir