编程创建的复选框上出现空对象引用错误

时间:2017-12-18 23:28:51

标签: android null findviewbyid

我有一个程序,可以根据先前活动中的用户输入以编程方式创建复选框。虽然复选框是正确的,但是当需要检查它们是否被检查时,我得到一个空对象引用错误。我非常清楚有类似的线程,但是它们都包含非常具体的答案,没有一个能解决我的问题。

 //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();
    }

运行此代码时,我在&#34; if(subChecked1.isChecked())&#34;上得到一个空对象引用。部分代码。

这里的参考是错误:

    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)

1 个答案:

答案 0 :(得分:0)

问题出在您的findViewById(box)和其他findViewById()方法调用中。你需要在这里提供一个xml元素id;比如findViewById(R.id.checkBox1)。它实际上解析为int值。我建议你阅读findViewById()。