使用DatePicker和TimePicker对话框 – Eclipse中的Android Java

我在代码上没有错误,但是当我启动时,我的主XML页面中出现致命错误,

03-21 22:00:31.849:E / AndroidRuntime(606):java.lang.RuntimeException:无法实例化活动ComponentInfo {countrycabin.ist236 / countrycabin.ist236.Main}:java.lang.NullPointerException

我不确定是什么导致了这个问题。 正如我所说,我没有错误。 我制作了一个非常类似的程序,工作得很好,我刚刚将TimePicker添加到这个程序中。

我想做的事情非常简单。 我正在启动程序,当我单击一个按钮时,会出现一个对话框,显示DatePicker或TimePicker,以及有人选择日期单词显示在底部的TextView中。 然而,我甚至无法测试它,因为它甚至不会发射。 我的模拟器告诉我它不会响应。

正如教科书中所述,我正在使用Honeycomb。

这是我的所有代码。

主要清单XML

            

主XML

               

主类(Java代码)

 package countrycabin.ist236; import java.util.Calendar; import android.os.Bundle; import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.TimePickerDialog; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.RadioButton; import android.widget.TextView; import android.widget.TimePicker; public class Main extends Activity { private int currentYear; private int currentMonth; private int currentDay; static final int DATE_DIALOG_ID = 0; static final int TIME_DIALOG_ID = 1; int hour; int minute; String cabin1 = "The Wooden Castle"; String cabin2 = "Cozy Little Spot"; private Button btDate; private Button btTime; private TextView timeDisplay; private TextView dateDisplay; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); timeDisplay = (TextView) findViewById(R.id.txtTime); dateDisplay = (TextView) findViewById(R.id.txtDate); btDate = (Button) findViewById(R.id.btnDate); btTime = (Button) findViewById(R.id.btnTime); btDate.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { showDialog(DATE_DIALOG_ID); } }); final Calendar c = Calendar.getInstance(); currentYear = c.get(Calendar.YEAR); currentMonth = c.get(Calendar.MONTH); currentDay = c.get(Calendar.DAY_OF_MONTH); btTime.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ showDialog(TIME_DIALOG_ID); } }); final Calendar d = Calendar.getInstance(); hour = d.get(Calendar.HOUR_OF_DAY); minute = d.get(Calendar.MINUTE); } protected Dialog onCreateDialog(int id) { switch(id){ case DATE_DIALOG_ID: return new DatePickerDialog(this, reservationDate, currentYear, currentMonth, currentDay); case TIME_DIALOG_ID: return new TimePickerDialog(this, timeDate, hour, minute, false); } return null; } private DatePickerDialog.OnDateSetListener reservationDate = new DatePickerDialog.OnDateSetListener() { final RadioButton c1 = (RadioButton) findViewById(R.id.radCabin1); final RadioButton c2 = (RadioButton) findViewById(R.id.radCabin2); @Override public void onDateSet(DatePicker view, int year, int month, int day){ if(c1.isChecked()){ dateDisplay.setText("Your rental time is set for " + (month + 1) + "-" + day + "-" + year + " to " + (month + 1) + "-" + (day + 3) + "-" + year + " in " + cabin1 + "."); } if(c2.isChecked()){ dateDisplay.setText("Your rental time is set for " + (month + 1) + "-" + day + "-" + year + " to " + (month + 1) + "-" + (day + 3) + "-" + year + " in " + cabin2 + "."); } } }; private TimePickerDialog.OnTimeSetListener timeDate = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hours, int minutes) { timeDisplay.setText("Your arrival time will be at " + hours + ":" + minutes + "."); } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

检查此代码:它现在正常工作。

在此处输入图像描述

我已经在XML文件以及您的活动中做了一些更改,现在它正常工作

但是你必须根据你的要求做出一些改变。

                              

活动:

 public class Main extends Activity { private int currentYear; private int currentMonth; private int currentDay; static final int DATE_DIALOG_ID = 0; static final int TIME_DIALOG_ID = 1; int hour; int minute; String cabin1 = "The Wooden Castle"; String cabin2 = "Cozy Little Spot"; private Button btDate; private Button btTime; private TextView timeDisplay; private TextView dateDisplay; private RadioButton c1; private RadioButton c2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Calendar c = Calendar.getInstance(); currentYear = c.get(Calendar.YEAR); currentMonth = c.get(Calendar.MONTH); currentDay = c.get(Calendar.DAY_OF_MONTH); hour = c.get(Calendar.HOUR_OF_DAY); minute = c.get(Calendar.MINUTE); timeDisplay = (TextView) findViewById(R.id.txtTime); dateDisplay = (TextView) findViewById(R.id.txtDate); btDate = (Button) findViewById(R.id.btnDate); btTime = (Button) findViewById(R.id.btnTime); c1 = (RadioButton) findViewById(R.id.radCabin1); c2 = (RadioButton) findViewById(R.id.radCabin2); btDate.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub showDialog(DATE_DIALOG_ID); } }); btTime.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(TIME_DIALOG_ID); } }); } protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: return new DatePickerDialog(this, reservationDate, currentYear, currentMonth, currentDay); case TIME_DIALOG_ID: return new TimePickerDialog(this, timeDate, hour, minute, false); } return null; } private DatePickerDialog.OnDateSetListener reservationDate = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int month, int day) { if (c1.isChecked()) { dateDisplay.setText("Your rental time is set for " + (month + 1) + "-" + day + "-" + year + " to " + (month + 1) + "-" + (day + 3) + "-" + year + " in " + cabin1 + "."); } if (c2.isChecked()) { dateDisplay.setText("Your rental time is set for " + (month + 1) + "-" + day + "-" + year + " to " + (month + 1) + "-" + (day + 3) + "-" + year + " in " + cabin2 + "."); } } }; private TimePickerDialog.OnTimeSetListener timeDate = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hours, int minutes) { timeDisplay.setText("Your arrival time will be at " + hours + ":" + minutes + "."); } }; public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }