当前位置: 代码迷 >> Android >> android NullPointerException闪退解决方法
  详细解决方案

android NullPointerException闪退解决方法

热度:37   发布时间:2016-04-28 06:37:16.0
android NullPointerException闪退
Override
protected Dialog onCreateDialog(int id) {
    Dialog dialog = null;
    switch (id) {
    case DATE_DIALOG:
        c = Calendar.getInstance();
        dialog = new DatePickerDialog(
            this,
            new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker dp, int year,int month, int dayOfMonth) {
                    dateBtn.setText("您选择了:" + year + "年" + (month+1) + "月" + dayOfMonth + "日");
                }
            }, 
            c.get(Calendar.YEAR), // 传入年份
            c.get(Calendar.MONTH), // 传入月份
            c.get(Calendar.DAY_OF_MONTH) // 传入天数
        );
        break;
    case TIME_DIALOG:
        c=Calendar.getInstance();
        dialog=new TimePickerDialog(
            this, 
            new TimePickerDialog.OnTimeSetListener(){
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    timeBtn.setText("您选择了:"+hourOfDay+"时"+minute+"分");
                }
            },
            c.get(Calendar.HOUR_OF_DAY),
            c.get(Calendar.MINUTE),
            false
        );
        break;
    }
    return dialog;


每当运行到
  dateBtn.setText("您选择了:" + year + "年" + (month+1) + "月" + dayOfMonth + "日");
的时候,就出现NullPointerException的问题,求解答
------解决方案--------------------

目测dateBtn为null,检查下dateBtn
------解决方案--------------------
正解!

引用:
目测dateBtn为null,检查下dateBtn

------解决方案--------------------
没有绑定buttonID
------解决方案--------------------
dateBtn  没初始化过吧。
------解决方案--------------------
dateBtn估计你只是定义了一下,没有findViewById(R.id.xxxx);
  相关解决方案