SpecialSnowflake
SpecialSnowflake

Reputation: 995

Nullpointer exception in fragment (initializing listview)

Here's my code for trying to display a list in a Fragment, which as of now keeps reporting a NullPointerException whenever it tries to initialize the ListView:

public class PortfolioFragment extends ListFragment {

ListView l;
ArrayAdapter<String> adapter;
List<String> list;

public PortfolioFragment() {
    // Required empty public constructor
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);                

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    list = MainActivity.globalArrayTest;
    l = (ListView) getView().findViewById(android.R.id.list);
    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
    l.setAdapter(adapter);

    return inflater.inflate(R.layout.fragment_portfolio, container, false);

}

I am getting an error at this line:

l = (ListView) getView().findViewById(android.R.id.list);

Upvotes: 0

Views: 1172

Answers (3)

Ojonugwa Jude Ochalifu
Ojonugwa Jude Ochalifu

Reputation: 27247

This is another way to go about this, although I extend only Fragment

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                                          Bundle SavedInstanceState) {

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


            ListView itemList = (ListView)rootView.findViewById(R.id.itemListView);


     String[] items = {"Alpha", "Orange", "Pineapple", "Venus", "Echo", "Pent", "Mouse", 
                      "Phoenix", "Dent", "Sloppy"};


         ArrayAdapter<String> adapter = new ArrayAdapter<String>(rootView.getContext(),
                      R.layout.text_row, items);

  itemList.setAdapter(new ArrayAdapter<>(rootView.getContext(), 
                             R.layout.fragment_item, items));

    itemList.setAdapter(adapter);


    return (rootView);
}

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

Change to

View v = inflater.inflate(R.layout.fragment_portfolio, container, false);
l = (ListView) v.findViewById(android.R.id.list);
return v; 

You should have the below in xml

<ListView android:id="@android:id/list"

Or override onActivityCreated and use getListView()

Also you can use the below as black belt commented this is better than the above.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
            l = getListView(); // since you extend ListFragment
}

Upvotes: 4

Blackbelt
Blackbelt

Reputation: 157447

since you are extending ListFragment you should use getListView() instead of findViewById and setListAdapter should be called after onCreateView (inside onActivityCreated, for instance)

Upvotes: 3

Related Questions