RadioGroup.setOnCheckedChangeListener无效并返回null?

时间:2017-09-25 15:24:55

标签: java android null radio-group jradiobutton

我的名字是Amaya,我是javaAndroid Studio的新人。我正在为我的最后一个班级项目建立一个专家系统,但我因为RadioButtonRadioGroups而被卡住了(说实话,我不知道哪些错误)。实际上,我想做一个简单的事情,如果我检查一些我的radiobuttons,我可以得到我可以在另一个布局中设置TextView的字符串。这是我的第一个布局:

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollView2" >
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_marginTop="10dp"
                android:text="Isilah formulir pengecekan kesehatan dibawah ini sesuai dengan keadaan sapi anda!"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textView3"
                android:layout_marginBottom="10dp"/>
            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="#AFAFAF"
                android:layout_marginRight="13dp"
                android:layout_marginLeft="2dp"
                android:layout_marginBottom="5dp"
                />
            <TextView
                android:text="Nafsu makan:"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/x1" />

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/a"
                android:onClick="onRGClick">

                <RadioButton
                    android:text="Normal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/a1" />

                <RadioButton
                    android:text="Tidak normal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/a2" />

            </RadioGroup>

            <TextView
                android:layout_marginTop="15dp"
                android:text="Cungur hidung:"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textView4" />
            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/b"
                android:onClick="onRGClick">
                <RadioButton
                    android:text="Basah"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/b1" />

                <RadioButton
                    android:text="Tidak basah"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/b2" />
            </RadioGroup>
            <TextView
                android:layout_marginTop="15dp"
                android:text="Feses sapi:"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textView3" />

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/c"
                android:onClick="onRGClick">
                <RadioButton
                    android:text="Tidak ada"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/c1" />

                <RadioButton
                    android:text="Normal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/c2" />

                <RadioButton
                    android:text="Lembek"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/c3" />

                <RadioButton
                    android:text="Mencret"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/c4" />

            </RadioGroup>
            <TextView
                android:layout_marginTop="15dp"
                android:text="Ada lendir dari hidung?"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textView3" />

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/d"
                android:onClick="onRGClick">
                <RadioButton
                    android:text="Ada"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/d1" />

                <RadioButton
                    android:text="Tidak"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/d2" />

            </RadioGroup>
            <TextView
                android:layout_marginTop="15dp"
                android:text="Frekuensi pernafasan:"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textView3" />

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/e"
                android:onClick="onRGClick">
                <RadioButton
                    android:text="Normal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/e1" />

                <RadioButton
                    android:text="Dipercepat"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/e2" />

            </RadioGroup>
            <Button
                android:layout_marginTop="15dp"
                android:text="Proses"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/proses"
                android:layout_weight="1.00"
                android:layout_gravity="center"
                android:background="@color/colorBackground"
                android:textColor="#FFFFFF"
                android:textStyle="bold" />


        </LinearLayout>
    </ScrollView>
</LinearLayout>

这是布局的java:

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
    public class sehat extends AppCompatActivity {
        public RadioGroup[] radio = new RadioGroup[14];
        public RadioButton[] radiobutton = new RadioButton[29];
        public RadioButton a1,a2,b1,b2,c1,c2,c3,c4,d1,d2,e1,e2,f1,f2,g1,g2,h1,h2,i1,i2,j1,j2,j3,k1,k3,k2,l1,l2,l3;
        public Button Kembali, proses;
        public String t0,t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.sehat);
            //iki();
            proses = (Button) findViewById(R.id.proses);
            //radiobutton
            a1 = (RadioButton) findViewById(R.id.a1);
            a2 = (RadioButton) findViewById(R.id.a2);
            b1 = (RadioButton) findViewById(R.id.b1);
            b2 = (RadioButton) findViewById(R.id.b2);
            c1 = (RadioButton) findViewById(R.id.c1);
            c2 = (RadioButton) findViewById(R.id.c2);
            c3 = (RadioButton) findViewById(R.id.c3);
            c4 = (RadioButton) findViewById(R.id.c4);
            d1 = (RadioButton) findViewById(R.id.d1);
            d2 = (RadioButton) findViewById(R.id.d2);
            e1 = (RadioButton) findViewById(R.id.e1);
            e2 = (RadioButton) findViewById(R.id.e2);
            f1 = (RadioButton) findViewById(R.id.f1);
            f2 = (RadioButton) findViewById(R.id.f2);
            g1 = (RadioButton) findViewById(R.id.g1);
            g2 = (RadioButton) findViewById(R.id.g2);
            h1 = (RadioButton) findViewById(R.id.h1);
            h2 = (RadioButton) findViewById(R.id.h2);
            i1 = (RadioButton) findViewById(R.id.i1);
            i2 = (RadioButton) findViewById(R.id.i2);
            j1 = (RadioButton) findViewById(R.id.j1);
            j2 = (RadioButton) findViewById(R.id.j2);
            j3 = (RadioButton) findViewById(R.id.j3);
            k1 = (RadioButton) findViewById(R.id.k1);
            k2 = (RadioButton) findViewById(R.id.k2);
            k3 = (RadioButton) findViewById(R.id.k3);
            l1 = (RadioButton) findViewById(R.id.l1);
            l2 = (RadioButton) findViewById(R.id.l2);
            l3 = (RadioButton) findViewById(R.id.l3);
            //RadioGroup
            radio[0] = (RadioGroup) findViewById(R.id.a);
            radio[1] = (RadioGroup) findViewById(R.id.b);
            radio[2] = (RadioGroup) findViewById(R.id.c);
            radio[3] = (RadioGroup) findViewById(R.id.d);
            radio[4] = (RadioGroup) findViewById(R.id.e);
            radio[5] = (RadioGroup) findViewById(R.id.f);
            radio[6] = (RadioGroup) findViewById(R.id.g);
            radio[7] = (RadioGroup) findViewById(R.id.h);
            radio[8] = (RadioGroup) findViewById(R.id.i);
            radio[9] = (RadioGroup) findViewById(R.id.j);
            radio[10] = (RadioGroup) findViewById(R.id.k);
            radio[11] = (RadioGroup) findViewById(R.id.l);
            proses.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    AlertDialog.Builder builder1 = new AlertDialog.Builder(sehat.this);
                    builder1.setMessage("Apakah anda sudah yakin dengan semua pilihan yang diisi, dan ingin melihat hasil konsultasi?");
                    builder1.setCancelable(true);

                    builder1.setPositiveButton(
                            "Ya",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    if ((a1.isChecked() || a2.isChecked()) && (b1.isChecked() || b2.isChecked()) && (c1.isChecked() || c2.isChecked() || c3.isChecked() || c4.isChecked()) && (d1.isChecked() || d2.isChecked()) && (e1.isChecked() || e2.isChecked()) && (f1.isChecked() || f2.isChecked()) && (g1.isChecked() || g2.isChecked()) && (h1.isChecked() || h2.isChecked()) && (i1.isChecked() || i2.isChecked()) && (j1.isChecked() || j2.isChecked() || j3.isChecked()) && (k1.isChecked() || k2.isChecked() || k3.isChecked()) && (l1.isChecked() || l2.isChecked() || l3.isChecked())) {
                                        radio[1].setOnCheckedChangeListener(new OnCheckedChangeListener() {
                                            @Override
                                            public void onCheckedChanged(RadioGroup group, int checkedId) {
                                                if (checkedId == R.id.a1) {
                                                    t1 = "i";
                                                } else if (checkedId == R.id.a2) {
                                                    t1 = "yo a2";
                                                    t0 = "Tidak Normal";

                                                }
                                            }
                                        });
                                        radio[2].setOnCheckedChangeListener(new OnCheckedChangeListener() {
                                            @Override
                                            public void onCheckedChanged(RadioGroup group, int checkedId) {
                                                if (checkedId == R.id.b1) {
                                                    t2 = "yo a1";
                                                    t0 = "Tidak Normal";
                                                } else if (checkedId == R.id.b2) {
                                                    t2 = "i";
                                                }
                                            }
                                        });
                                        radio[3].setOnCheckedChangeListener(new OnCheckedChangeListener() {
                                            @Override
                                            public void onCheckedChanged(RadioGroup group, int checkedId) {
                                                if (checkedId == R.id.c1) {
                                                    t3 = "yo a1";
                                                    t0 = "Tidak Normal";
                                                } else if (checkedId == R.id.c2) {
                                                    t3 = "i";
                                                }
                                                else if (checkedId == R.id.c3) {
                                                    t3 = "yo a2";
                                                    t0 = "Tidak Normal";
                                                }
                                                else if (checkedId == R.id.c4){
                                                    t3 = "yo a2";
                                                    t0 = "Tidak Normal";

                                                }
                                            }
                                        });
                                        radio[4].setOnCheckedChangeListener(new OnCheckedChangeListener() {
                                            @Override
                                            public void onCheckedChanged(RadioGroup group, int checkedId) {
                                                if (checkedId == R.id.d1) {
                                                    t4 = "yo a1";
                                                    t0 = "Tidak Normal";
                                                } else if (checkedId == R.id.d2) {
                                                    t4 = "i";
                                                }
                                            }
                                        });
                                        radio[5].setOnCheckedChangeListener(new OnCheckedChangeListener() {
                                            @Override
                                            public void onCheckedChanged(RadioGroup group, int checkedId) {
                                                if (checkedId == R.id.e1) {
                                                    t5 = "i";
                                                } else if (checkedId == R.id.e2) {
                                                    t5 = "";
                                                    t0 = "Tidak Normal";
                                                }
                                            }
                                        });
                                        radio[6].setOnCheckedChangeListener(new OnCheckedChangeListener() {
                                            @Override
                                            public void onCheckedChanged(RadioGroup group, int checkedId) {
                                                if (checkedId == R.id.f1) {
                                                    t6 = "yo yo";
                                                    t0 = "Tidak Normal";
                                                } else if (checkedId == R.id.f2) {
                                                    t6 = "i";
                                                }
                                            }
                                        });
                                        radio[7].setOnCheckedChangeListener(new OnCheckedChangeListener() {
                                            @Override
                                            public void onCheckedChanged(RadioGroup group, int checkedId) {
                                                if (checkedId == R.id.g1) {
                                                    t7 = "yo yo";
                                                    t0 = "Tidak Normal";
                                                } else if (checkedId == R.id.g2) {
                                                    t7 = "i";
                                                }
                                            }
                                        });
                                        radio[8].setOnCheckedChangeListener(new OnCheckedChangeListener() {
                                            @Override
                                            public void onCheckedChanged(RadioGroup group, int checkedId) {
                                                if (checkedId == R.id.h1) {
                                                    t8 = "yo yo";
                                                    t0 = "Tidak Normal";
                                                } else if (checkedId == R.id.h2) {
                                                    t8 = "i";
                                                }
                                            }
                                        });
                                        radio[9].setOnCheckedChangeListener(new OnCheckedChangeListener() {
                                            @Override
                                            public void onCheckedChanged(RadioGroup group, int checkedId) {
                                                if (checkedId == R.id.i1) {
                                                    t9 = "i";
                                                } else if (checkedId == R.id.i2) {
                                                    t9 = "yo yo";
                                                    t0 = "Tidak Normal";
                                                }
                                            }
                                        });
                                        radio[10].setOnCheckedChangeListener(new OnCheckedChangeListener() {
                                            @Override
                                            public void onCheckedChanged(RadioGroup group, int checkedId) {
                                                if (checkedId == R.id.j1) {
                                                    t10 = "i";
                                                } else if (checkedId == R.id.j2) {
                                                    t10 = "yo yo";
                                                    t0 = "Tidak Normal";
                                                }
                                                else if (checkedId == R.id.j3) {
                                                    t10 = "yo yo";
                                                    t0 = "Tidak Normal";
                                                }
                                            }
                                        });
                                        radio[11].setOnCheckedChangeListener(new OnCheckedChangeListener() {
                                            @Override
                                            public void onCheckedChanged(RadioGroup group, int checkedId) {
                                                if (checkedId == R.id.k1) {
                                                    t11 = "i";
                                                } else if (checkedId == R.id.k2) {
                                                    t11 = "yo yo";
                                                    t0 = "Tidak Normal";
                                                }
                                                else if (checkedId == R.id.k3) {
                                                    t11 = "yo yo";
                                                    t0 = "Tidak Normal";
                                                }
                                            }
                                        });
                                        radio[12].setOnCheckedChangeListener(new OnCheckedChangeListener() {
                                            @Override
                                            public void onCheckedChanged(RadioGroup group, int checkedId) {
                                                if (checkedId == R.id.l1) {
                                                    t12 = "i";
                                                } else if (checkedId == R.id.l2) {
                                                    t12 = "yo yo";
                                                    t0 = "Tidak Normal";
                                                }
                                                else if (checkedId == R.id.l3){
                                                    t12 = "yo yo";
                                                    t0 = "Tidak Normal";
                                                }
                                            }
                                        });

                                        Intent i = new Intent(getApplicationContext(), Informasi_Sehat.class);
                                        startActivity(i);
                                    } else {
                                        Toast.makeText(getApplicationContext(), "Anda harus menjawab semua pertanyaan terlebih dahulu", Toast.LENGTH_LONG).show();
                                    }

                                }
                            });
                    builder1.setNegativeButton(
                            "Belum",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();
                                }
                            });

                    AlertDialog alert11 = builder1.create();
                    alert11.show();
                }
            });
            Button btn_kembalihome = (Button) findViewById(R.id.kembalihomed);
            btn_kembalihome.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    finish();
                }
            });
        }
    }

错误信息是:

  

尝试调用虚方法'void   android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup $ OnCheckedChangeListener)”   在空对象引用上

添加radio[1].setOnCheckedChangeListener bla bla bla时出现程序错误。我希望你能帮助我。如果需要,这是我的结果java。

public class Informasi_Sehat extends Activity {
    public TextView tt0,tt1,tt2,tt3,tt4,tt5,tt6,tt7,tt8,tt9,tt10,tt11,tt12;
    public String t0,t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.informasi_sehat);
        iku();
        ((Button) findViewById(R.id.btn_kembalihome)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                finish();
            }
        });
    }

    public void iku(){
        tt0 = (TextView) findViewById(R.id.namaSpecies);
        tt1 = (TextView) findViewById(R.id.namaDeskripsi1);
        tt2 = (TextView) findViewById(R.id.namaDeskripsi2);
        tt3 = (TextView) findViewById(R.id.namaDeskripsi3);
        tt4 = (TextView) findViewById(R.id.namaDeskripsi4);
        tt5 = (TextView) findViewById(R.id.namaDeskripsi5);
        tt6 = (TextView) findViewById(R.id.namaDeskripsi6);
        tt7 = (TextView) findViewById(R.id.namaDeskripsi7);
        tt8 = (TextView) findViewById(R.id.namaDeskripsi8);
        tt9 = (TextView) findViewById(R.id.namaDeskripsi9);
        tt10 = (TextView) findViewById(R.id.namaDeskripsi10);
        tt11 = (TextView) findViewById(R.id.namaDeskripsi11);
        tt12 = (TextView) findViewById(R.id.namaDeskripsi12);
        tt0.setText(String.valueOf(t0));
        tt1.setText(String.valueOf(t1));
        tt2.setText(String.valueOf(t2));
        tt3.setText(String.valueOf(t3));
        tt4.setText(String.valueOf(t4));
        tt5.setText(String.valueOf(t5));
        tt6.setText(String.valueOf(t6));
        tt7.setText(String.valueOf(t7));
        tt8.setText(String.valueOf(t8));
        tt9.setText(String.valueOf(t9));
        tt10.setText(String.valueOf(t10));
        tt11.setText(String.valueOf(t11));
        tt12.setText(String.valueOf(t12));
    }



}

谢谢。如果我的语法不好,我很抱歉。

1 个答案:

答案 0 :(得分:0)

您是否正在使用Java开发专家系统?有可能吗?