Question

Null Pointer Exception in line:

   listView.setAdapter(sAdapter);

public class MyEmployeFragment extends Fragment {


    MyTask task;
    String employer_id;
    String employer_name;
    String str=null;
    // имена атрибутов для Map
    final String ATTRIBUTE_ID   = "p_id";
    final String ATTRIBUTE_NAME = "p_name";
    ListView listView;

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

        //myPrefs = this.getActivity().getSharedPreferences("MYsettings", Context.MODE_PRIVATE);
        task    = new MyTask();
        task.execute();

        // Creating view correspoding to the fragment
        View v = inflater.inflate(R.layout.my_employe, container, false);

        // Updating the action bar title
        getActivity().getActionBar();
        String[] userId = {"1","2","3"};

        String[] userName = {"Nadezhd","Vasya", "hardcore"};

        ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>(
                userId.length);
        Map<String, Object> m;
        for (int i = 0; i < userId.length; i++) {
            m = new HashMap<String, Object>();
            m.put(ATTRIBUTE_ID, userId[i]);
            m.put(ATTRIBUTE_NAME, userName[i]);
            data.add(m);
        }

        // массив имен атрибутов, из которых будут читаться данные
        String[] from = {ATTRIBUTE_ID, ATTRIBUTE_NAME};
        // массив ID View-компонентов, в которые будут вставлять данные
        int[] to = {R.id.tw_employe_id, R.id.tw_employe_name};

        // создаем адаптер
        SimpleAdapter sAdapter = new SimpleAdapter(getActivity(), data, R.layout.list_item_employee,
                from, to);


        // определяем список и присваиваем ему адаптер
        listView = (ListView) getActivity().findViewById(R.id.lv_employee_list);


       // ListView listView = (ListView) getView();
        listView.setAdapter(sAdapter);
    return v;
}



        class MyTask extends AsyncTask<Void, Void, Void> {

            @Override
            protected void onPreExecute() {

                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... params) {

                String s = "5ACACEC6-752B-4EFF-AA50-EEBE58A52113";
                // String user_guid = myPrefs.getString("guid", "");

                HttpActivity _http = new HttpActivity("192.168.10.11", "80");
                _http.set_addr_protocol("/WebSite/P/spr/spr.aspx/");
                _http.add_param("query", "spr_employee_get");
                // _http.add_param("p_guid", user_guid.toString().trim());
                _http.add_param("p_guid", s);
                _http.send();

                employer_name = _http.getArrayParamValue("p_name");
                employer_id = _http.getArrayParamValue("p_id");


                // массивы данных
                //String[] userId = {employer_id};

                //String[] userName = {employer_name};



            return null;
        }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);


            }
        }
Was it helpful?

Solution

As @Little Child mentioned, your ListView is null, and problem is the following line of code:

listView = (ListView) getActivity().findViewById(R.id.lv_employee_list);

which should really be:

listView = (ListView) v.findViewById(R.id.lv_employee_list);

OTHER TIPS

From the looks of it, your listView is null and not your adapter. Print it to the logcat and see what you get. :)

Update based on Egor's answer:
What you have inside an Activity is a Fragment. So when you look for the list view via getActivity().findViewById(), Android will search for the ListView in the Activity and it will not find it there because the ListView is in the Fragment.

Hence, you need to use the inflated view of the fragment and use findElementbyId() and get your mysteriously elusive ListView :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top