Android listview getchildAt()的view.findViewById返回空指针
这是我从JSON数据中获取的代码,我将它传递给一个数组Android listview getchildAt()的view.findViewById返回空指针,android,listview,nullpointerexception,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> arrayst
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);
// }
}
}
对不起,我的英语不好,谢谢
我的错误日志
使用适配器查看位置 而不是:
View view = questionAnswerList.getChildAt(position);
如果你发布完整的代码,我会帮助你修复代码
希望对你有所帮助我想你通过了I+1的位置。 尝试更改
changebackgroundpreselect(i+1)代码>至<代码>变更背景预选(i)代码>
发布完整代码以确定确切的问题请添加完整的java和错误代码…@W4R10CK我已更新了错误日志,但我认为问题是从View=questionAnswerList.getChildAt(位置)开始的;此处请添加完整java代码列表中的每个项目是否都有复选框?questionAnswerList
在位置position
处没有子项。这就是你得到NPE的原因。如果questionAnswerList
是一个列表视图,那么出现此错误并不奇怪。您需要了解ListView
如何工作我的源代码我在这里发布源代码,您在哪里向ArrayStoreResponse列表添加项目,是否尝试了上述更改?ArrayStoreResponse列表只是一个大的arraylist,用于检查它是否存在,如果具有相同的值,然后运行该函数以更改问题回答列表中的复选框OK,但您在其中添加项目的位置..您是否尝试将i+1替换为i?emm,我跳过了步骤dy,bcoz将项目添加到arraystoreanswer不是主要问题。是的,我尝试过替换它,但仍然有错误,我放置i+1的目的是我的listview包含列表页眉和列表页脚,因此项目从1开始计数
View view = questionAnswerList.getChildAt(position);