报错log
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hgfenis.radiogroupdemo01, PID: 7180
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hgfenis.radiogroupdemo01/com.hgfenis.radiogroupdemo01.RadioGroupActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2668)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2729)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1480)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6198)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)' on a null object reference
at com.hgfenis.radiogroupdemo01.RadioGroupActivity.onCreate(RadioGroupActivity.java:31)
at android.app.Activity.performCreate(Activity.java:6692)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2621)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2729)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1480)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6198)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
报错行代码
radiogroup.setOnCheckedChangeListener(this);
报错原因:
对 ButterKnife.bind(this); 的使用不熟练
解决办法:
一、使用普通控件绑定方法即解决问题
public class RadioGroupActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
private String TAG = RadioGroupActivity.class.getSimpleName();
/*@BindView(R.id.activity_main_radiogroup)
RadioGroup radiogroup;
@BindView(R.id.activity_main_radiobutton_home)
RadioButton activity_main_radiobutton_home;*/
private RadioGroup radiogroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_group);
/*ButterKnife.bind(this);*/
radiogroup = (RadioGroup)findViewById(R.id.activity_main_radiogroup);
radiogroup.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.activity_main_radiobutton_home:
Log.i(TAG,"首页");
break;
case R.id.activity_main_radiobutton_classification:
Log.i(TAG,"商品");
break;
case R.id.activity_main_radiobutton_activity:
Log.i(TAG,"分类");
break;
case R.id.activity_main_radiobutton_mine:
Log.i(TAG,"我的");
break;
default:
break;
}
}
}
报错原因:
对 ButterKnife.bind(this); 的使用不熟练,只添加依赖包,没有添加配置
解决办法:
按照官方文档添加配置
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
//ButterKnife
classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.3'
}
}
android {
...
// Butterknife requires Java 8.
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
...
//ButterKnife
implementation "com.jakewharton:butterknife:10.2.3"
annotationProcessor "com.jakewharton:butterknife-compiler:10.2.3"
}