Home » Android » Cannot be cast to android.app.Fragment

Cannot be cast to android.app.Fragment

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m just trying to have a look on Fragment and I got some probs with stupid stuff…
There is my SkippersActivity.java :

public class SkippersActivity extends Activity{
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    setContentView(R.layout.skippers_fragment);
 }
}

And my skippers_fragment.xml :

 <?xml version="1.0" encoding="utf-8"?>
    <fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.arkezis.globedroid.SkippersFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/skippers_fragment">
    </fragment>

And there is my SkippersFragment.xml :

public class SkippersFragment extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    View mainView = inflater.inflate(R.layout.skippers, container, false);      
    return mainView;
}   

}

My skippers.xml :

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >    
    <ListView android:id="@+id/list_skippers_all"
        android:layout_width="match_parent"
    android:layout_height="match_parent">
    </ListView>
    </LinearLayout>

Why do I got :

04-30 13:45:16.355: E/AndroidRuntime(30077): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.arkezis.globedroid/com.arkezis.globedroid.SkippersActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.os.Looper.loop(Looper.java:137)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.app.ActivityThread.main(ActivityThread.java:4424)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at java.lang.reflect.Method.invokeNative(Native Method)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at java.lang.reflect.Method.invoke(Method.java:511)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at dalvik.system.NativeStart.main(Native Method)
04-30 13:45:16.355: E/AndroidRuntime(30077): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.app.Activity.setContentView(Activity.java:1835)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at com.arkezis.globedroid.SkippersActivity.onCreate(SkippersActivity.java:19)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.app.Activity.performCreate(Activity.java:4465)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-30 13:45:16.355: E/AndroidRuntime(30077):    ... 11 more
04-30 13:45:16.355: E/AndroidRuntime(30077): Caused by: java.lang.ClassCastException: com.arkezis.globedroid.SkippersFragment cannot be cast to android.app.Fragment
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.app.Fragment.instantiate(Fragment.java:574)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.app.Fragment.instantiate(Fragment.java:549)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.app.Activity.onCreateView(Activity.java:4235)
04-30 13:45:16.355: E/AndroidRuntime(30077):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
How to&Answers:

If I had to guess, SkippersFragment is extending android.support.v4.app.Fragment. However, SkippersActivity is not extending android.support.v4.app.FragmentActivity. So SkippersActivity is trying to use native API Level 11 fragments.

Answer:

If you use the v4 compatibility package, your Activity has to extend FragmentActivity, not Activity. If you use Android > 3.0, then your Activity extends Activity, but read CommonsWare’s answer about not mixing both types of fragments.

Answer:

Another pitfall to watch out for is to make sure you don’t forget to call super.onCreate() from your activity. If you make this dumb mistake like I did, you’ll also see the “Error inflating class fragment” and “… cannot be cast to android.app.Fragment” errors, even if you haven’t mismatched the standard and support.v4 versions of the classes.

Answer:

I ran into the same error while trying to extend ListFragment. My finding is that, to use ListFragment on a project with minSdkVersion < 11, I will have to import android.support.v4.app.ListFragment;. Otherwise, I will have to import android.app.ListFragment;.

This link clarified my question.