java.lang.NullPointerException onDateSet

时间:2015-01-14 23:24:15

标签: java android alarm

任何人都可以帮忙,为什么我会得到这个例外!!!

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,点击每个按钮会抛出这种异常。想知道发生了什么!!!

P.S ---- Reminder.java:290是这行代码:editDate.setText(dateStr);

1 个答案:

答案 0 :(得分:1)

这一行

editTime=(EditText)findViewById(R.id.edt_time);
当您确实需要查看setContentView()

中夸大的布局时,

告诉它查看您使用Dialog虚张声势的布局

将其更改为

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