Java 尝试对空对象引用、无线组调用虚方法
我添加了这个单选组,带有单选按钮,尽管现在我发现我的整个单选组都是空的?一件非常有趣的事情是,当我在API为22的设备上运行时,我的应用程序没有检测到这个错误,但是当我在28API模拟器上重新运行它时,这个错误确实出现了。 代码如下:Java 尝试对空对象引用、无线组调用虚方法,java,android,Java,Android,我添加了这个单选组,带有单选按钮,尽管现在我发现我的整个单选组都是空的?一件非常有趣的事情是,当我在API为22的设备上运行时,我的应用程序没有检测到这个错误,但是当我在28API模拟器上重新运行它时,这个错误确实出现了。 代码如下: radioGroup = findViewById(R.id.radioGroup2); Rd1= findViewById(R.id.radioButton5); Rd2= findViewById(R.id.radioButton6);
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>
这是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'
}
}
}
}
您的RadioGroup具有id=“RadioGroup 2” 在代码中,您尝试查找“radioGroup” 只需将其更改为:
radioGroup = findViewById( R.id.radioGroup2 );
它显示空指针异常。检查它所指的线,找出空对象。@Ravi我不明白它说什么,你能帮忙吗?看起来它找不到你的广播组。检查您是否已使用正确的id@Ravi我有一个人,在XML上,射线组的名称是“射线组2”,它对应于Java的名称,请检查您的代码。您已发布为“radioGroup=findViewById(R.id.radioGroup);”。它在XML和活动中应该是相同的。您可以重新执行活动吗,所以我希望单击单选按钮并开始一个新的活动activity@TomasMota更新的代码显示单击的按钮。你可以在识别点击时执行startActivity()。我尝试过,尽管,它仍然显示相同的错误,有什么建议吗?我尝试过,尽管,它仍然显示相同的错误,有什么建议吗?此外,每当我在较低的API设备上运行应用程序时,它运行得非常好,只有在22 API或更高的设备上运行时,才会出现此错误。
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;
}
}
}
});
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'
android:id="@+id/radioGroup2"
radioGroup = findViewById( R.id.radioGroup );
radioGroup = findViewById( R.id.radioGroup2 );