java.lang.nullpointerexception ondateset

ff29svar  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(508)

谁能帮我解释一下为什么我会得到这个例外!!!

01-15 00:15:51.380  24298-24298/creotive.walleta3 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at creotive.walleta3.Reminder$5.onDateSet(Reminder.java:290)
            at android.app.DatePickerDialog.tryNotifyDateSet(DatePickerDialog.java:199)
            at android.app.DatePickerDialog.onClick(DatePickerDialog.java:154)
            at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5306)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)

我的代码如下:

protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
setContentView(R.layout.reminder_layout);
        editDate=(EditText)findViewById(R.id.edt_date);   
         setReminderButt = (Button) findViewById(R.id.setReminderButt);

                    setReminderButt.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        dialog = new Dialog(Reminder.this);
                        dialog.setContentView(R.layout.activity_dialog1);
                        dialog.setTitle("Reminder Alarm");

                         editTime=(EditText)findViewById(R.id.edt_time);

                        setButton=(Button)dialog.findViewById(R.id.btn_set);
                        date=(Button)dialog.findViewById(R.id.date_picker);
                        time=(Button)dialog.findViewById(R.id.time_picker);

                        date.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {

                                Toast.makeText(Reminder.this, "Reagon", Toast.LENGTH_SHORT).show();
                                final Calendar c = Calendar.getInstance();
                                myYear = c.get(Calendar.YEAR);
                                myMonth = c.get(Calendar.MONTH);
                                myDay = c.get(Calendar.DAY_OF_MONTH);

                                showDialog(ID_DATEPICKER);

                            }
                        });
                        time.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {

                                final Calendar c = Calendar.getInstance();
                                myHour = c.get(Calendar.HOUR_OF_DAY);
                                myMinute = c.get(Calendar.MINUTE);

                                showDialog(ID_TIMEPICKER);

                            }
                        });
                        setButton.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                requestCode=(int)System.currentTimeMillis();
                                try{
                                    msg=editMessage.getText().toString();
                                }catch (Exception e) {
                                    msg="";
                                }
                                dateTime=dateStr.trim()+" "+timeStr.trim();
                                try {
                                    SimpleDateFormat formatter = new SimpleDateFormat( "dd-MM-yyyy HH:mm" );
                                    setDate=(Date)formatter.parse(dateTime);
                                } catch (ParseException e){

                                }
                                al=new Alarm();
                                al.SetAlarm(Reminder.this, msg, requestCode, setDate, dateTime);
                                Toast.makeText(getApplicationContext(), "Alarm has set", Toast.LENGTH_LONG).show();
                            }
                        });

                        dialog.show();

                    }
                    });
        }

            @Override
            protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
                switch(id){
                    case ID_DATEPICKER:

                        return new DatePickerDialog(this,
                                myDateSetListener,
                                myYear, myMonth, myDay);
                    case ID_TIMEPICKER:

                        return new TimePickerDialog(this,
                                myTimeSetListener,
                                myHour, myMinute, false);
                    default:
                        return null;

                }
            }

            private DatePickerDialog.OnDateSetListener myDateSetListener
                    = new DatePickerDialog.OnDateSetListener(){

                @Override
                public void onDateSet(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {

                    dateStr=String.valueOf(dayOfMonth)+"-"+String.valueOf(monthOfYear+1)+"-"+String.valueOf(year);

                       editDate.setText(dateStr);

                }
            };

            private TimePickerDialog.OnTimeSetListener myTimeSetListener
                    = new TimePickerDialog.OnTimeSetListener(){

                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                    timeStr=String.valueOf(hourOfDay)+":"+String.valueOf(minute);

                        editTime.setText(timeStr);

                }
            };

场景是这样的:我有一个按钮setreminder,当我点击它时,它会显示一个对话框,可以设置提醒的时间和日期。这个对话框还有两个按钮settime和setdate,单击它们就会抛出这种异常。idk发生了什么事!!!
p、 s——提醒。java:290 is 这行代码:editdate.settext(datestr);

oxf4rvwz

oxf4rvwz1#

这条线在这里

editTime=(EditText)findViewById(R.id.edt_time);

告诉它看看你的布局 setContentView() 当你真的需要查看 Dialog 把它改成

editTime=(EditText) dialog.findViewById(R.id.edt_time);

相关问题