Unable to access a list view within a static nested class which extends fragment

I am referring this sample provided by google: Navigation Drawer sample

I am trying to show all moons of Saturn in a list view whenever user selects planet Saturn.

For this I updated below method in outer MainActivity class:

private void selectItem(int position) {

        Fragment fragment;

        switch (position) {
            case 5: {
                // update the main content by replacing fragments

                fragment = new MoonsListFragment();

            }
            break;
            default: {

                fragment = new PlanetFragment();
                Bundle args = new Bundle();
                args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
                fragment.setArguments(args);
            }
        }


        FragmentManager fragmentManager = getFragmentManager();
        if (fragment != null){
            fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
        }


        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        setTitle(mPlanetTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

Added this as nested class:

public static class MoonsListFragment extends Fragment {

        private ListView mMoonsList;
        private String[] mMoonsTitles;

        public MoonsListFragment(){

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            mMoonsTitles = getResources().getStringArray(R.array.saturn_moons_array);

            View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
            mMoonsList = (ListView)rootView.findViewById(R.id.moons_list); // mMoonsList value is null, though moons_list is available in R.java


            // set up the moons list view with items and click listener
            mMoonsList.setAdapter(new ArrayAdapter<String>(getActivity(),
                    R.layout.moons_list_item, mMoonsTitles));
            //mMoonsList.setOnItemClickListener(new DrawerItemClickListener());

            return rootView;
        }
    }

Below is content from R.java file:

package com.example.android.navigationdrawerexample;

public final class R {
    public static final class array {
        public static final int planets_array=0x7f040000;
        public static final int saturn_moons_array=0x7f040001;
    }
    public static final class attr {
    }
    public static final class drawable {
        public static final int action_search=0x7f020000;
        public static final int drawer_shadow=0x7f020001;
        public static final int earth=0x7f020002;
        public static final int ic_drawer=0x7f020003;
        public static final int ic_launcher=0x7f020004;
        public static final int jupiter=0x7f020005;
        public static final int mars=0x7f020006;
        public static final int mercury=0x7f020007;
        public static final int neptune=0x7f020008;
        public static final int saturn=0x7f020009;
        public static final int uranus=0x7f02000a;
        public static final int venus=0x7f02000b;
    }
    public static final class id {
        public static final int action_websearch=0x7f070005;
        public static final int content_frame=0x7f070001;
        public static final int drawer_layout=0x7f070000;
        public static final int image=0x7f070004;
        public static final int left_drawer=0x7f070002;
        public static final int moons_list=0x7f070003;
    }
    public static final class layout {
        public static final int activity_main=0x7f030000;
        public static final int drawer_list_item=0x7f030001;
        public static final int fragment_moons_list=0x7f030002;
        public static final int fragment_planet=0x7f030003;
        public static final int moons_list_item=0x7f030004;
    }
    public static final class menu {
        public static final int main=0x7f060000;
    }
    public static final class string {
        public static final int action_websearch=0x7f050000;
        public static final int app_name=0x7f050001;
        public static final int app_not_available=0x7f050002;
        public static final int drawer_close=0x7f050003;
        public static final int drawer_open=0x7f050004;
    }
}

fragment_planet.xml :

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:gravity="center"
    android:padding="32dp" />

For some reasons mMoonsList is coming to be null, on which I am clueless 🙁

Any ideas?

Few more inputs – my app is crashing at below line:

mMoonsList.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.moons_list_item, mMoonsTitles));

Answer

you need to return rootView instead mMoonsList

boy. replese

View rootView = inflater.inflate(R.layout.fragment_planet, container, false);

for that

View rootView = inflater.inflate(R.layout.fragment_moons_list, container, false);

Leave a Reply

Your email address will not be published. Required fields are marked *