1

I face a NullPointerException while setting the ArrayAdapter on the ListView.

The code and Exceptions are mentioned below. Since the data is being provided in the list .. i know its not NULL. Then why the issue ....any help or pointers really appreciated !

Here is the code: MainActivityFragment.java

public class MainActivityFragment extends Fragment {

    public MainActivityFragment() {
}

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

    View view = inflater.inflate(R.layout.fragment_main, container, false);

    List list = new ArrayList<String>();
    list.add("Today - Sunny - 88/63");
    list.add("Tomorrow - Foggy - 88/63");
    list.add("Wednesday - Cloudy - 88/63");
    list.add("Thursday - Rainy - 88/63");
    list.add("Friday - Sunny - 88/63");

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),
            R.layout.fragment_main,
            R.id.list_item_forecast_textview,
            list);

    ListView listView = (ListView) view.findViewById(R.id.list_view_forecast);
    listView.setAdapter(arrayAdapter);

    return view;

}
}

Exception:

java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.ListView.makeAndAddView(ListView.java:1790)
at android.widget.ListView.fillDown(ListView.java:691)
at android.widget.ListView.fillFromTop(ListView.java:752)
at android.widget.ListView.layoutChildren(ListView.java:1630)

1 Answer 1

4

You're wrong here.

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),
        R.layout.fragment_main,
        R.id.list_item_forecast_textview,
        list);

at R.layout.fragment_main

You should pass the ListItem Layout here

 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),
        R.layout.yourListItemLayout,
        R.id.list_item_forecast_textview,
        list);
Sign up to request clarification or add additional context in comments.

2 Comments

Damn ! Could'nt believe i did that silly mistake ! Thanks a ton
can you tell me how is an answer supposed to be accepted ?

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.