0

I'm trying to use a ListView in a fragment. But getActivity() is null. I have already searched and found many threads about this topic but these were not very helpful.
Here is my code:

ListView.java:

package de.listview.example;
import android.app.Activity;
import android.content.Context;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

public class ListViews extends ListFragment {

    private AdapterView.OnItemClickListener listener;

    public void makelist()
    {
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                   "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                   "Linux", "OS/2" };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);
    }

}

MainActivity.java:

public static class ListFragment extends Fragment
{

    Context mContext;

    @Override
    public void onAttach(Activity activity) {
        mContext = getActivity();
        Log.i("Event", "onAttach called");
        super.onAttach(activity);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        ListViews lst = new ListViews();
        lst.makelist();
        return inflater.inflate(R.layout.whitelist_content, container, false);
    }


}

case 2:
    fragmentManager.beginTransaction()
                .replace(R.id.content_frame, new ListFragment())
                .commit();
    break;
TN888
  • 7,659
  • 9
  • 48
  • 84
Björn Ternes
  • 1,137
  • 4
  • 14
  • 33
  • you should carefully read the documentation for fragment and activity – Blackbelt Jul 27 '13 at 13:10
  • 1
    `getActivity()` is null because your `Fragment` has not yet been attached to an `Activity`, which means the layout hasn't even inflated yet. This is standard behavior. You should familiarize yourself with the [Fragment lifecycle](http://developer.android.com/guide/components/fragments.html#Lifecycle). – arkon Jul 27 '13 at 13:30

1 Answers1

1

In ListFragment in the onAttach() method your fragment still isn't attached to any Activity that's why you're getting a null value.

Change:

mContext = getActivity();

To:

mContext = activity;

Or use getActivity() inside onCreateView() for example.


The other problem is you are creating your fragment but it is not attached to any Activity when you use makeList() that's why you are getting a null value when using getActivity().

The right way to do that is for example call makeList() inside onCreateView() of your ListView fragment.

Also after creating the fragment you have to add it with:

fragmentManager.beginTransaction().replace(R.id.content_frame, lst).commit();

You should read about how to work with fragments: http://developer.android.com/guide/components/fragments.html

Andres
  • 6,080
  • 13
  • 60
  • 110