-2
以前のアクティビティのユーザー入力に基づいてプログラムでチェックボックスを作成するプログラムがあります。チェックボックスが正しく行われている間に、チェックされているかどうかを確認する時間が来たら、Nullオブジェクト参照エラーが発生します。私は非常に特定の答えが含まれていますが、私の問題を解決するものはありませんが、同様のスレッドがあることを非常に認識しています。プログラムで作成されたチェックボックスでNullオブジェクト参照エラーが発生する
//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)