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 FrameLayout
s 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!
Change this
to
Edit:
And