【问题标题】:Android Studio: Passing RadioButtons using Intent ERROR NullPointerExceptionAndroid Studio:使用 Intent ERROR NullPointerException 传递 RadioButtons
【发布时间】:2016-04-10 01:12:15
【问题描述】:

我是初学者,所以我提前道歉 运行此程序时出现以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getCheckedRadioButtonId()' on a null object reference

这是我的“测验”代码

public class Quiz extends Activity {
Button btn;
RadioGroup rg;
RadioButton rb1;
RadioButton rb2;
RadioButton rb3;
RadioButton rb4;

然后在下面:2

还有“测验”的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" />

第二个活动(测验1):3

感谢您的任何帮助

【问题讨论】:

  • 如果您只是将所有相关代码而不是图像发布到问题中,将会有所帮助。让人们更难理解您的代码并帮助您解决问题。
  • 你是对的!感谢您的反馈:)

标签: java android-studio nullpointerexception radio-button radio-group


【解决方案1】:

问题是,您没有在 Quiz1 类中初始化 RadioGroup 按钮 radioGroup

【讨论】:

  • 还是报错:java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getCheckedRadioButtonId()' on a null object reference
【解决方案2】:

RadioGroup rg 在调用 getCheckedRadioButtonId() 之前需要一个初始化步骤来检查 RadioButton。

像这样:

RadioGroup rg;
RadioButton rb1;
rg = ....
rb1 = ....
// check a RadioButton first
rg.check(R.id.rb1);

并确保您在“findViewByid”方法中的 id 与 RadioGroup 在 Layout Xml 中的 id 相似。

【讨论】:

  • 还是报错:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.doesntmatter.choosecharacter/com.doesntmatter.choosecharacter.Quiz}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.check(int)' on a null object reference
  • 你能告诉我测验活动使用的那个xml文件吗?
  • 错误消息表示您尚未创建 RadioGroup 实例,因此:( 。当您的 Layout xml 中缺少 RadioGroup 或使用错误的 id 时,可能会发生这种情况。
  • 我在上面的原始帖子中添加了用于测验的 XML(所以是第一个活动)
  • 查看 xml 和 RadioGroup 的 id 是 rg,但在您的代码中“findViewByID(R.id.rg1)”@JessicaAdams
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-20
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
  • 1970-01-01
相关资源
最近更新 更多