编程创建的复选框上出现空对象引用错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编程创建的复选框上出现空对象引用错误相关的知识,希望对你有一定的参考价值。
我有一个程序,可以根据之前活动中的用户输入以编程方式创建复选框。虽然复选框是正确的,但是当需要检查它们是否被检查时,我得到一个空对象引用错误。我非常清楚有类似的线程,但是它们都包含非常具体的答案,没有一个能解决我的问题。
//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())”部分得到一个空对象引用。
这里的参考是错误:
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()。
以上是关于编程创建的复选框上出现空对象引用错误的主要内容,如果未能解决你的问题,请参考以下文章
以编程方式创建 MapView 并添加标记导致片段中出现空指针异常