Dies ist der Code, den ich von JSON-Daten nehmen, i übergibt es an einen Arrayview.findViewById für Listview getChildAt() return Nullpointer
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);
// }
}
}
Dies ist meine Leere Funktion, weiß, dass ich das Hauptproblem hier ist, aber weiß nicht, wie es zu lösen, GetView die Listenansicht null zurück die Position ich es, aber zurückkehren Nullpointer an hier:
CheckBox cb = (CheckBox) view.findViewById(R.id.answer_content);
Sorry für mein Englisch gebrochen, danke Mein Fehlerprotokoll enter image description here
fügen Sie bitte vollständig java und Fehlercode versuchen
changebackgroundpreselect(i+1);
-changebackgroundpreselect(i);
Post vollständigen Code zu ändern ... – W4R10CK
@ W4R10CK habe ich mein Fehlerprotokoll aktualisieren, aber ich denke, das Problem von View starten view = questionAnswerList.getChildAt (Position); hier –
Bitte füge vollen Java-Code hinzu –