elbaquero
elbaquero

Reputation: 81

ClassCastException: CustomFragment cannot be cast to android.app.Fragment / Using android.support.v4.app.Fragment

I am developing an application divided in one library and two applications : 1 for phones, the other for tablets. A lot of code and layout is defined in the library, and only a few parts are defined in the applications.

I'm using fragments in an Activity, with a ViewPager for the phone version (layout and activity defined in the library, used with no change in the phone application). For the tablet version, I want to show my fragments (2) side by side, and not in a ViewPager, so I tried to build an XML layout like that :

<....>
    <LinearLayout
        android:id="@+id/fragmentsParent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <fragment
            android:id="@+id/f1"
            android:class="com.test.Fragment1"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <fragment
            android:id="@+id/f2"
            android:name="com.test.Fragment2"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"  />
    </LinearLayout>
</....>

My problem is that the code is working on my phone application, but not on my tablet application. On this one, I get this exception stack :

07-11 17:41:14.032: E/AndroidRuntime(14754):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
07-11 17:41:14.032: E/AndroidRuntime(14754):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1782)
07-11 17:41:14.032: E/AndroidRuntime(14754):    ... 11 more

07-11 17:41:14.032: E/AndroidRuntime(14754): Caused by: java.lang.ClassCastException: com.test.Fragment1 cannot be cast to android.app.Fragment

07-11 17:41:14.032: E/AndroidRuntime(14754):    at android.app.Fragment.instantiate(Fragment.java:560)
07-11 17:41:14.032: E/AndroidRuntime(14754):    at android.app.Fragment.instantiate(Fragment.java:535)
07-11 17:41:14.032: E/AndroidRuntime(14754):    at android.app.Activity.onCreateView(Activity.java:4168)
07-11 17:41:14.032: E/AndroidRuntime(14754):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664)

The error messages talk about android.app.Fragment where I Would expect android.support.v4.app.Fragment.

After searching the web, I checked these points :

Upvotes: 8

Views: 9513

Answers (4)

pj.nowakowski
pj.nowakowski

Reputation: 1

I've just came across the same problem, in my case calling super.onCreate(Bundle) before setting layout with setContentView(int) solved the ClassCastException

Upvotes: 0

DoruAdryan
DoruAdryan

Reputation: 1334

Have you solved this in any way? If not, you should check out your manifest file, if there is an Activity declared with your Fragments name. (if so, remove them)

Upvotes: 0

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

You probably got wrong (mixed) imports and instad of

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

you have

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;

hence the cast issue.

Upvotes: 2

leonvian
leonvian

Reputation: 4769

Your Activity have to be extended by FragmentActivity.

Upvotes: 6

Related Questions