我有一个程序,该程序根据事先活动中的用户输入以编程方式创建复选框。当正确制作复选框时,当需要检查它们是否被检查时,我会收到一个空对象参考错误。我非常清楚,有类似的线程,但是所有线程都包含非常具体的答案,也没有解决我的问题。
//this part is in the OnCreate portion of the activity
String [] winQuest;
winQuest = getResources().getStringArray(R.array.windowQuestions);
for (int i = 0; i < winQuest.length; i++) {
TableRow row = new TableRow(this);
//row.setId(i);
row.setLayoutParams(basicParams);
TextView questionText = new TextView(this);
questionText.setText(winQuest[i]);
questionText.setTextColor(getColor(R.color.black));
questionText.setId(R.id.questionText + i);
row.addView(questionText);
TableRow row2 = new TableRow(this);
//row2.setId(i);
row2.setLayoutParams(basicParams);
CheckBox checkBox = new CheckBox(this);
String boxId = i + "1000";
int id = Integer.parseInt(boxId);
checkBox.setId(id);
checkBox.setText(simpleAns[0]);
row2.addView(checkBox);
//subChecks.add(checkBox);
TableRow row3 = new TableRow(this);
//row3.setId(i);
row3.setLayoutParams(basicParams);
CheckBox checkBox2 = new CheckBox(this);
String boxId2 = i + "2000";
int id2 = Integer.parseInt(boxId2);
checkBox.setId(id2);
checkBox2.setText(simpleAns[1]);
row3.addView(checkBox2);
//subChecks.add(checkBox2);
TableRow row4 = new TableRow(this);
//row4.setId(i);
row4.setLayoutParams(basicParams);
CheckBox checkBox3 = new CheckBox(this);
String boxId3 = i + "3000";
int id3 = Integer.parseInt(boxId3);
checkBox.setId(id3);
checkBox3.setText(simpleAns[2]);
row4.addView(checkBox3);
//subChecks.add(checkBox3);
TableRow row5 = new TableRow(this);
//row5.setId(i);
row5.setLayoutParams(basicParams);
EditText addtlNotesText = new EditText(this);
addtlNotesText.setHint(getResources().getString(R.string.notes));
String textId4 = i + "4000";
int id4 = Integer.parseInt(textId4);
checkBox.setId(id4);
row5.addView(addtlNotesText);
noteText.add(addtlNotesText);
rootLayout.addView(row);
rootLayout.addView(row2);
rootLayout.addView(row3);
rootLayout.addView(row4);
rootLayout.addView(row5);
}
//this part happens OnClick
String [] winQuest;
winQuest = getResources().getStringArray(R.array.windowQuestions);
for(int i = 0; i < winQuest.length; i++){
fieldNoteResponses = "";
String subCheck = i + "1000";
String subCheck2 = i + "2000";
String subCheck3 = i + "3000";
int box = Integer.parseInt(subCheck);
final CheckBox subChecked1 = findViewById(box);
int box2 = Integer.parseInt(subCheck2);
final CheckBox subChecked2 = findViewById(box2);
int box3 = Integer.parseInt(subCheck3);
final CheckBox subChecked3 = findViewById(box3);
if (subChecked1.isChecked()) {
fieldNoteResponses = "Yes";
}
if (subChecked2.isChecked()){
fieldNoteResponses = "No";
}
if (subChecked3.isChecked()) {
fieldNoteResponses = "Not Applicable";
}
String addNotes = "";
String textCheck = i + "4000";
int textId = Integer.parseInt(textCheck);
final EditText addText = findViewById(textId);
addNotes = addText.getText().toString();
}
运行此代码时,我在" if(subchecked1.ischecked())"一部分上获得了null对象引用。
在这里参考是错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: redcaribou.abbphotoorganizerv2, PID: 4942
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.CheckBox.isChecked()' on a null object reference
at redcaribou.abbphotoorganizerv2.FirstCheckList.onClick(FirstCheckList.java:394)
at android.view.View.performClick(View.java:5623)
at android.view.View$PerformClick.run(View.java:22433)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6314)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
问题在您的findViewById(box)
和其他FindViewById()方法调用中。您需要在此处提供XML元素ID;像findViewById(R.id.checkBox1)
。它实际上解决了int
值。我建议您在findviewbyid()中进行一些阅读。