[英]Android Studio: Passing RadioButtons using Intent ERROR NullPointerException
I am a beginner so i apologize in advance I get the following Error when running this program: 我是一个初学者,所以我向您道歉,在运行此程序时出现以下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getCheckedRadioButtonId()' on a null object reference
here is my code for "Quiz" 这是我的“测验”代码
public class Quiz extends Activity {
Button btn;
RadioGroup rg;
RadioButton rb1;
RadioButton rb2;
RadioButton rb3;
RadioButton rb4;
And the xml for "quiz": 和“测验”的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="@+id/rg">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1"
android:id="@+id/rb1"
android:layout_marginBottom="55dp"
android:layout_above="@+id/rb2"
android:layout_centerHorizontal="true"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2"
android:id="@+id/rb2"
android:layout_above="@+id/rb3"
android:layout_alignLeft="@+id/rb1"
android:layout_alignStart="@+id/rb1"
android:layout_marginBottom="58dp"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 3"
android:id="@+id/rb3"
android:layout_above="@+id/rb4"
android:layout_alignLeft="@+id/rb2"
android:layout_alignStart="@+id/rb2"
android:layout_marginBottom="57dp"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 4"
android:id="@+id/rb4"
android:layout_marginBottom="71dp"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/rb3"
android:layout_alignStart="@+id/rb3"
android:checked="false" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Question:"
android:id="@+id/textView3"
android:layout_marginTop="39dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="@+id/nextBtn"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
Second activity (Quiz1): 3 第二次活动(Quiz1): 3
any help is appreciated thank you lots <3 感谢任何帮助,谢谢您<3
RadioGroup rg need a init step to check a RadioButton before invoke getCheckedRadioButtonId(). RadioGroup rg需要一个初始化步骤来检查RadioButton,然后再调用getCheckedRadioButtonId()。
Like this: 像这样:
RadioGroup rg;
RadioButton rb1;
rg = ....
rb1 = ....
// check a RadioButton first
rg.check(R.id.rb1);
And make sure that your id in "findViewByid" method is similar to RadioGroup's id in Layout Xml. 并确保“ findViewByid”方法中的id与Layout Xml中的RadioGroup的id相似。
问题是,您没有在Quiz1
类中初始化RadioGroup按钮radioGroup 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.