0

I have an activity managing a list of fragments. A new fragment I added is extending ListFragmet instead of Fragment, and I added it in the activity xml layout file as the other fragments. I'm getting an InflatingException from at this file at the row where I declare the ListFragment. What is wrong with this declaration?

Activity XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <fragment android:name="com.example.foodexp01b.HomeFragment"
          android:id="@+id/homeFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />
    <fragment android:name="com.example.foodexp01b.LoginFragment"
          android:id="@+id/loginFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />
    <fragment android:name="com.example.foodexp01b.SettingsFragment"
          android:id="@+id/settingsFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

    <fragment android:name="com.example.foodexp01b.DestinationFragment"
          android:id="@+id/destinationFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />
    <fragment
          android:id="@+id/mapFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.SupportMapFragment"/>

    <fragment android:name="com.example.foodexp01b.RestaurantFragment"
          android:id="@+id/restaurantFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

    <fragment android:name="com.example.foodexp01b.FavoritesFragment"
          android:id="@+id/favoritesFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

</LinearLayout>

Activity OnCreate code

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        uiHelper = new UiLifecycleHelper(this, callback);
        uiHelper.onCreate(savedInstanceState);

        FragmentManager fm = getSupportFragmentManager();
        fragments[LOGIN] = fm.findFragmentById(R.id.loginFragment);
        fragments[HOME] = fm.findFragmentById(R.id.homeFragment);
        fragments[SETTINGS] = fm.findFragmentById(R.id.settingsFragment);
        fragments[SETTINGS].getView().findViewById(R.id.back_button1)
        .setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                currentFragment = HOME;
                showFragment(HOME, false);
            }
        });
        fragments[DESTINATIONS] = fm.findFragmentById(R.id.destinationFragment);
        fragments[DESTINATIONS].getView().findViewById(R.id.back_button2)
        .setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                currentFragment = HOME;
                showFragment(HOME, false);
            }
        });
        fragments[MAP] = fm.findFragmentById(R.id.mapFragment);
        fragments[RESTAURANT] = fm.findFragmentById(R.id.restaurantFragment);
        fragments[RESTAURANT].getView().findViewById(R.id.restaurant_back_button)
        .setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                currentFragment = MAP;
                showFragment(MAP, false);
            }
        });
        fragments[FAVORITES].getView().findViewById(R.id.favoritesFragment);
        FragmentTransaction transaction = fm.beginTransaction();
        for(int i = 0; i < fragments.length; i++) {
            transaction.hide(fragments[i]);
        }
        transaction.commit();
    }

FavoritesFragment OnCreateView code

@Override
    public View onCreateView(LayoutInflater inflater, 
            ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(android.R.layout.simple_list_item_1, container, false);
        favorites = new ArrayList<String>();
        setListAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, favorites));

        getListView().setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
                }
            });
        return view;
    }

Log

06-30 20:17:53.609: E/AndroidRuntime(675): FATAL EXCEPTION: main
06-30 20:17:53.609: E/AndroidRuntime(675): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.foodexp01b/com.example.foodexp01b.MainActivity}: android.view.InflateException: Binary XML file line #36: Error inflating class fragment
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2063)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.app.ActivityThread.access$600(ActivityThread.java:134)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.os.Looper.loop(Looper.java:137)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.app.ActivityThread.main(ActivityThread.java:4744)
06-30 20:17:53.609: E/AndroidRuntime(675):  at java.lang.reflect.Method.invokeNative(Native Method)
06-30 20:17:53.609: E/AndroidRuntime(675):  at java.lang.reflect.Method.invoke(Method.java:511)
06-30 20:17:53.609: E/AndroidRuntime(675):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-30 20:17:53.609: E/AndroidRuntime(675):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-30 20:17:53.609: E/AndroidRuntime(675):  at dalvik.system.NativeStart.main(Native Method)
06-30 20:17:53.609: E/AndroidRuntime(675): Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class fragment
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-30 20:17:53.609: E/AndroidRuntime(675):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:262)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.app.Activity.setContentView(Activity.java:1867)
06-30 20:17:53.609: E/AndroidRuntime(675):  at com.example.foodexp01b.MainActivity.onCreate(MainActivity.java:118)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.app.Activity.performCreate(Activity.java:5008)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2027)
06-30 20:17:53.609: E/AndroidRuntime(675):  ... 11 more
06-30 20:17:53.609: E/AndroidRuntime(675): Caused by: java.lang.IllegalStateException: Content view not yet created
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
06-30 20:17:53.609: E/AndroidRuntime(675):  at com.example.foodexp01b.FavoritesFragment.onCreateView(FavoritesFragment.java:63)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:884)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1168)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:280)
06-30 20:17:53.609: E/AndroidRuntime(675):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
06-30 20:17:53.609: E/AndroidRuntime(675):  ... 21 more
ezy
  • 59
  • 1
  • 9
  • 25
  • Use LogCat to examine the Java stack trace associated with your exception. If you cannot interpret what the stack trace is telling you, edit your question and post the stack trace (e.g., use `Ctrl-C` to copy highlighted lines from DDMS's LogCat view to the clipboard). – CommonsWare Jun 30 '13 at 18:18
  • Yes sorry, forgot to post the log – ezy Jun 30 '13 at 18:24

1 Answers1

1

You are calling getListView() in onCreateView(). The problem is that the ListView is created in onCreateView(), so until you return from onCreateView(), the superclass does not have the ListView yet.

I would recommend that you move your onCreateView() code to onActivityCreated(), by which time the ListView will have been created. Also, you can get rid of that view local variable, as you are not using it. And, if FavoritesFragment is a ListFragment, instead of calling setOnItemClickListener(), override onListItemClick() in your fragment.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you, I've moved the code to onActivityCreated(). But eclipse tells me "The method setOnListItemClick(new OnListItemClick(){}) is undefined for the type ListView" with code getListView().setOnListItemClick – ezy Jun 30 '13 at 18:33
  • @user2265222: There is no `setOnListItemClick()` method on `ListView`. I suggested **overriding** `onListItemClick()` **on your fragment**, if that fragment is a `ListFragment`: http://developer.android.com/reference/android/app/ListFragment.html#onListItemClick(android.widget.ListView, android.view.View, int, long) – CommonsWare Jun 30 '13 at 18:35
  • Got it, but in the main activity I have to manage which fragment to show and which one to hide and (predictably with your explanation) I'm getting a null exception when picking up the fragment on the activity create. Should I invoke a callback from the ListFragment to the activity to set it at the right time? – ezy Jun 30 '13 at 18:50
  • @user2265222: I am sorry, but I do not understand the nature of your problem. You might consider opening a fresh StackOverflow question, with updated code and a new stack trace, to go along with an expanded explanation of what you are trying and where the problem is occurring. – CommonsWare Jun 30 '13 at 18:57