java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.RadioGroup.set

报错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"
}

参考文档

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值