当前位置: 首页 > 知识库问答 >
问题:

尝试对空对象引用、无线电组调用虚拟方法

洪浩
2023-03-14
radioGroup = findViewById(R.id.radioGroup2);
    Rd1= findViewById(R.id.radioButton5);
    Rd2= findViewById(R.id.radioButton6);
    radioGroup.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (Rd1.isChecked()) {
                attemptLogin();
                Intent intent = new Intent(LoginActivity.this, BottomActivity.class);
                startActivity(intent);
            }else {
                if(Rd2.isChecked()) {
                    attemptLogin();
                    Intent intent = new Intent(LoginActivity.this, LoginActivity2.class);
                    startActivity(intent);
                }
            }

        }
    } );
<RadioGroup
            android:id="@+id/radioGroup2"
            android:layout_width="361dp"
            android:layout_height="125dp"
            android:layout_gravity="bottom"
            android:background="@color/licolor"
            android:gravity="center"
            android:layout_marginTop="20dp"
            android:orientation="vertical"
            tools:ignore="UselessParent">

            <RadioButton
                android:id="@+id/radioButton5"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_marginTop="16dp"
                android:background="@drawable/mybutton"
                android:gravity="center"
                android:text="@string/register"
                android:textColor="#fafafa"
                android:textStyle="bold"
                android:textSize="19sp"
                android:button="@null"/>

            <RadioButton
                android:id="@+id/radioButton6"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_marginTop="16dp"
                android:background="@drawable/mybutton"
                android:gravity="center"
                android:text="@string/login"
                android:textColor="#fafafa"
                android:textStyle="bold"
                android:textSize="19sp"
                android:button="@null"/>

        </RadioGroup>
Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)' on a null object reference

共有1个答案

楚德辉
2023-03-14

您的RadioGroup的id=“RadioGroup2”

android:id="@+id/radioGroup2"

在代码中,您试图找到“radiogroup”

radioGroup = findViewById( R.id.radioGroup );

只需将其更改为:

radioGroup = findViewById( R.id.radioGroup2 );
 类似资料: