0

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);
2
  • post the logcat please Commented Dec 17, 2013 at 14:31
  • you should use the inflater to find your view. See @Raghunandan 's answer Commented Dec 17, 2013 at 14:35

3 Answers 3

4

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

getListView is the best solution in this case
3

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

Comments

0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.