31

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)
Arkezis
  • 416
  • 1
  • 5
  • 9
  • Can you please paste the import statements of SkippersFragment.java? – Rajesh Apr 30 '12 at 12:04
  • import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; – Arkezis Apr 30 '12 at 12:36
  • Please take a look at http://stackoverflow.com/a/5404643/1321873 and http://stackoverflow.com/a/5584218/1321873. – Rajesh Apr 30 '12 at 12:49
  • Its not good to name your class with name that is so similiar. Can be confusing. – akemalFirdaus Oct 22 '12 at 07:51

4 Answers4

101

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • You are 100% right... BUT, They should have simply forced fragments to be used in an extension jar... this is a really stupid requirement, this throws backward compatibility down the drain... – TacB0sS May 18 '13 at 12:19
  • 1
    @TacB0sS: Talented programmers *increase* backwards compatibility by using the Android Support package. – CommonsWare May 18 '13 at 21:59
  • Yeh, I know that is what I do... problem starts when others which develop 3rd party infrastructure doesn't use the v4 package, and develop for newer OS versions and above simply because it has Fragments build into the OS. Then you cannot use that feature for older versions, and if to take for example, The Map v2 Service... it has two type of fragments one for OS Fragment usage, and one for v4 usage... brilliant! – TacB0sS May 19 '13 at 06:45
  • 2
    @TacB0sS: Hence, the fragments backport does not "throw backward compatibility down the drain". Poorly written "3rd party infrastructure" can "throw backward compatibility down the drain". – CommonsWare May 19 '13 at 07:54
  • Agreed! I was merely hinting(loudly), that some poor judgement allowed development branching such as this, and I find it terrible... – TacB0sS May 20 '13 at 07:54
  • 8
    people coming here in the future should note that you need to use getSupportFragmentManager() instead of getFragmentManager() when using the support package or you'll get the same issue as well. – MrTristan Jun 18 '13 at 18:54
  • Nice Hint, I wonder there is no such clue in developers Guide. Really Pathetic!:-( – sai Jan 30 '14 at 06:23
  • 1
    @CommonsWare you are a genius I had done the same thing and was cracking my head looking at the code you saved my day – Illegal Argument Jun 11 '14 at 11:38
19

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.

Christine
  • 5,617
  • 4
  • 38
  • 61
2

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.

dfinn
  • 968
  • 1
  • 10
  • 16
  • Not only that, but you have to call it *before* calling `setContentView` if your fragment is in the xml layout, which your answer helped me discover. – Andrew Kirkegaard Feb 05 '15 at 20:06
0

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.

Community
  • 1
  • 1
Dino Tw
  • 3,167
  • 4
  • 34
  • 48