我添加了这个无线电组,其中是radiobuttons,尽管现在我得到了整个无线电组为null?一个非常有趣的是,当我在API为22的设备上运行该错误时,我的应用不会检测到此错误,但是当我在28API模拟器上重新运行它时,此错误确实会出现。 这是代码:
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);
}
}
}
} );
这是XML代码,我尝试在我的登录活动中添加radiogroup视图,以便将两个按钮彼此放置,尽管一旦我这样做,我立即就会遇到此错误。
<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
您的radiogroup具有ID =" radiogroup2"
android:id="@+id/radioGroup2"
在代码中,您正在尝试找到" radiogroup"
radioGroup = findViewById( R.id.radioGroup );
只需将其更改为:
radioGroup = findViewById( R.id.radioGroup2 );
//XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 1"/>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 2"/>
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 3"/>
</RadioGroup>
//活动
RadioGroup radioGroup;
RadioButton RD1;
RadioButton RD2;
RadioButton RD3;
radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
RD1 = (RadioButton)findViewById(R.id.radioButton1);
RD2 = (RadioButton)findViewById(R.id.radioButton2);
RD3 = (RadioButton)findViewById(R.id.radioButton3);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedButtonId) {
// This will get the radiobutton that has changed in its check state
RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(checkedButtonId);
// This puts the value (true/false) into the variable
boolean isChecked = checkedRadioButton.isChecked();
// If the radiobutton that has changed in check state is now checked...
if (isChecked)
{
switch (checkedButtonId)
{
case R.id.radioButton1:
Log.i("test" , "Checked Button 1 ");
break;
case R.id.radioButton2:
Log.i("test" , "Checked Button 2 ");
break;
case R.id.radioButton3:
Log.i("test" , "Checked Button 3 ");
break;
}
}
}
});
,您有Gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.shrinkio"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-vector-drawable:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'android.arch.lifecycle:extensions:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-core:16.0.7'
implementation 'com.google.firebase:firebase-auth:16.1.0'
implementation "com.google.android.gms:play-services-gcm:16.1.0"
implementation 'com.android.support:cardview-v7:28.0.0'
}
apply plugin: 'com.google.gms.google-services'