getActivity().findViewById(R.layout.contacts_list_view) returns null

4.2k views Asked by At

I have seen some similar question about this but it was all for different reasons (I think). The code I am using is taken from http://developer.android.com/training. I am trying to build a simple contacts app according to the tutorial: http://developer.android.com/training/contacts-provider/retrieve-names.html The part where I am getting the bug is Set up the CursorAdapter for the ListView. This is my code:

@Override
    public void onActivityCreated(Bundle savedInstanceState) 
    {
        super.onActivityCreated(savedInstanceState);
        mContactsList = (ListView) getActivity().findViewById(R.layout.contacts_list_view);
        mCursorAdapter = new SimpleCursorAdapter(getActivity(), 
                R.layout.contacts_list_item, null, FROM_COLUMNS, TO_COLUMN, 0);
        mContactsList.setAdapter(mCursorAdapter);
        mContactsList.setOnItemClickListener(this);
        getLoaderManager().initLoader(0, null, this);
    }

All the XML files have been put in place:

contact_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@android:id/text1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:clickable="true"/>

contact_list_view.xml:

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

My main activity consists of 2 FrameLayouts for the fragments to be put in. This is it's XML:

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

    <FrameLayout
        android:id="@+id/frag_contacts"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:textColor="#0a20a3" >
    </FrameLayout>

    <FrameLayout 
        android:id="@+id/frag_contact_details"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#045011" />

</LinearLayout>

This is the onCreate of the activity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ninth);
        if (findViewById(R.id.frag_contacts) != null)
        {
            if (savedInstanceState != null)
                return;
            ContactsFragment contactsFrag = new ContactsFragment();
            getFragmentManager().beginTransaction().add(R.id.frag_contacts, contactsFrag).commit();
        }
    }

also, this is the onCreateView of the fragment:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.contacts_list_view, container, false);
    }

Now, sorry for the long post, some of it might be unnecessary, but the problem is that on the first snippet, getActivity().findViewById(R.layout.contacts_list_view) returns null all the time! Don't know what else to do. how else can I load data into the list, and what is wrong here? Thanks in advance!

6

There are 6 answers

2
Raghunandan On

Change this

mContactsList = (ListView) getActivity().findViewById(R.layout.contacts_list_view);

to

mContactsList = (ListView) getView().findViewById(R.id.contacts_list_view);

Edit:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+idlist"

And

 mContactsList = (ListView) getView().findViewById(R.id.list);
11
Blackbelt On

you have to change

mContactsList = (ListView) getActivity().findViewById(R.layout.contacts_list_view);

with

mContactsList = (ListView) getView().findViewById(R.id.contacts_list_view);

Since the ListView belongs to the View you return inside onCreateView.

0
Christopher Francisco On

You have to wait till the Fragment is attached to the Activity. You can do that onAttach(Activity) in your Fragment class

0
Dehan Wjiesekara On

replace

mContactsList = (ListView) getActivity().findViewById(R.layout.contacts_list_view);

to

mContactsList = (ListView) getView().findViewById(R.id.list);

contact_list_view.xml shoud be change as follows

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="**@+id/list**"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>
0
Samvel Kartashyan On

If you are trying to do getActivity().findViewById(R.id.contacts_list_view) in onCreateView, it will return null, because the View of Fragment is not created yet. Try to do that in onResume and it will return the view:

@Override
public void onResume() {
    super.onResume();
    mContactsList = (ListView) getActivity().findViewById(R.id.contacts_list_view);
 }
0
AdamHsi On

Fragment state will be saved in FragmentActivity, so you should overwrite onSaveInstanceState in your activity.

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if(outState != null) {
        String FRAGMENTS_TAG = "android:support:fragments";
        outState.remove(FRAGMENTS_TAG);
    }
}