我自定义的多选框可以选择,但是一点击确认(也就是文中的yes)就闪退了呢?

我自定义的多选框可以选择,但是一点击确认(也就是文中的yes)就闪退了呢?

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class CheckDialog extends Dialog {
    private TextView tv_gethobby,tv_title;
    private String msg = null;
    private CheckBox cb_pro,cb_music,cb_cook,cb_run;
    private SetCheckbox scb;
    private Button yes;
    private List<String> lists = new ArrayList<String>();

    public CheckDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        setContentView(R.layout.checkbox_dialog);

        cb_pro = findViewById(R.id.cb_pro);
        cb_music = findViewById(R.id.cb_music);
        cb_cook = findViewById(R.id.cb_cook);
        cb_run = findViewById(R.id.cb_run);
        yes = findViewById(R.id.yes);

        tv_gethobby = findViewById(R.id.tv_gethobby);
        tv_title = findViewById(R.id.tv_title);

        scb = new SetCheckbox();
        cb_pro.setOnCheckedChangeListener(scb);
        cb_music.setOnCheckedChangeListener(scb);
        cb_cook.setOnCheckedChangeListener(scb);
        cb_run.setOnCheckedChangeListener(scb);

        yes.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if(!lists.isEmpty()) {
                    tv_title.setText("个人爱好:");
                    tv_gethobby.setText(lists.toString());
                    lists.clear();
                }
                dismiss();
            }
        });
    }


    class SetCheckbox implements CompoundButton.OnCheckedChangeListener{
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            CheckBox checkBox = (CheckBox) buttonView;
            switch (checkBox.getId()){
                case R.id.cb_pro:
                        lists.add(cb_pro.getText().toString()+"\n");
                    break;
                case R.id.cb_music:
                        lists.add(cb_music.getText().toString()+"\n");
                    break;
                case R.id.cb_cook:
                        lists.add(cb_cook.getText().toString()+"\n");
                    break;
                case R.id.cb_run:
                        lists.add(cb_run.getText().toString()+"\n");
                    break;
            }
        }
    }
}


正在回答

登陆购买课程后可参与讨论,去登陆

3回答

你检查一下checkbox_dialog布局文件中是否有如下这两个控件id对应的TextView

http://img1.sycdn.imooc.com//climg/5cac77f300013eb604050069.jpg

提问者 LexieMIZUKI 2019-04-09 18:30:20
irista23 2019-04-09 12:11:05

你点击yes时看下Android Studio底部Logcat中红色的错误信息,或者把错误信息贴出来方便老师准确帮你查找问题原因

  • 提问者 LexieMIZUKI #1
    好的,谢谢老师,我把截图发在上面了。我把那个tv_title的findviewbyid写在settext的上方它也显示空指针,原来是和其他的放在一个initview里面也是空指针。希望老师帮我看看!
    2019-04-09 18:31:53
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师