listview的view.findViewById getchildAt()返回nullpointer

时间:2017-01-10 08:10:25

标签: android listview nullpointerexception

这是我从JSON数据中获取的代码,我将其传递给数组

public class CheckboxMCQ extends Fragment {

private View rootview;
private View footerView;
private View header;
private ListView questionAnswerList;
CheckboxAdapter checkboxAdapter;
ArrayList<checkboxmodel> arraystoreanswer;
ArrayList<checkboxmodel> arraytemppreselect;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    rootview = inflater.inflate(R.layout.fragment_checkbox_mcq, container, false);

    questionAnswerList = (ListView) rootview.findViewById(R.id.question_answer_list);
    footerView = ((LayoutInflater) getParentFragment().getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer, null,false);
    header = ((LayoutInflater) getParentFragment().getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.header, null,false);
    questionAnswerList.addFooterView(footerView);
    questionAnswerList.addHeaderView(header);
    arraystoreanswer = new ArrayList<checkboxmodel>();
    arraytemppreselect = new ArrayList<checkboxmodel>();

    checkboxAdapter = new CheckboxAdapter(getActivity(), arraystoreanswer,CheckboxMCQ.this , ss_id);
    questionAnswerList.setAdapter(checkboxAdapter);


    return rootview;
}


/*preselect operation*/
public void preselectoperation(final String uid, final String sf_id, final String ssid, final String sqid, final String order) {

    class AsyncTaskRunner extends AsyncTask<String,String,JSONObject> {
        String response;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected JSONObject doInBackground(String... params) {
            urlLink url = new urlLink();
            JSONObject jsonObject = url.checkboxpreselectrow(uid, sf_id, ssid, sqid);
            return jsonObject;
        }

        @Override
        protected void onPostExecute(JSONObject jsonObject) {
            super.onPostExecute(jsonObject);
            try {
                response = jsonObject.getString("success");
                if (response.equals("1"))
                {
                    JSONArray job = jsonObject.getJSONArray("record");
                    for (int i = 0; i<job.length(); i++){
                        JSONObject jsonOb = job.getJSONObject(i);
                        arraytemppreselect.add(new checkboxmodel(jsonOb.getString("ans_id")));
                    }

                    for (int i=0; i < arraystoreanswer.size(); i++){

                        for (int j=0; j < arraytemppreselect.size(); j++){
                            if((arraystoreanswer.get(i).getAnswerid().equals(arraytemppreselect.get(j).getAnswerid()))){
                                //do something for equals
                                Log.i("same value",arraystoreanswer.get(i).getAnswerid() );
//                                    Log.i("position value",""+ i );
                                changebackgroundpreselect(i+1);
                            }
                        }
                    }
                }
                else {
                }
            } catch (JSONException e1) {
                e1.printStackTrace();
            }
        }
    }
    new AsyncTaskRunner().execute();
}

public void changebackgroundpreselect(int position){
//            if (i == position){
    View view = questionAnswerList.getChildAt(position);
    Log.i("getChildAt",""+ position);
            CheckBox cb = (CheckBox) view.findViewById(R.id.answer_content);
            cb.setBackgroundResource(R.drawable.checkboxbuttonselected);
//            }
}
}

这是我的void函数,我知道主要的问题在这里,但dunno如何解决它,listview getview返回null 我得到它的位置,但在这里返回nullpointer:

CheckBox cb = (CheckBox) view.findViewById(R.id.answer_content);

抱歉我的英语不好,谢谢 我的错误日志 enter image description here

2 个答案:

答案 0 :(得分:0)

使用适配器获取位置

的视图

而不是:

 View view = questionAnswerList.getChildAt(position);

如果您发布完整代码,我会帮助修复代码 希望它有所帮助

答案 1 :(得分:0)

我认为你正在传递位置i + 1。 尝试将changebackgroundpreselect(i+1);更改为changebackgroundpreselect(i);

发布完整代码以确定确切的问题