在Eclipse中使用Android Java中的DatePicker和TimePicker对话框
我在代码上没有错误,但是当我启动时,在我的主XML页面中出现了一个致命错误,上面说
03-21 22:00:31.849: E/AndroidRuntime(606): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{countrycabin.ist236/countrycabin.ist236.Main}: java.lang.NullPointerException
我不确定是什么导致了这个问题。正如我所说,我没有错误。我制作了一个与此非常相似的程序,运行良好,我只是在这个程序中添加了时间选择器
我想做的很简单。我正在启动程序,当我单击一个按钮时,会显示一个对话框,显示日期选择器或时间选择器,当有人选择日期时,会在底部的文本视图中显示日期词。然而,我甚至无法测试它,因为它甚至不会启动。我的模拟器告诉我它不会响应
我用蜂巢来做这个,就像教科书上说的那样
这是我所有的代码
主清单XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="countrycabin.ist236"
安卓:versionCode="1"
安卓:versionName="1.0" >
<uses-sdk
安卓:minSdkVersion="8"
安卓:targetSdkVersion="11" />
<application
安卓:allowBackup="true"
安卓:icon="@drawable/ic_launcher"
安卓:label="@string/app_name"
安卓:theme="@style/AppTheme" >
<activity
安卓:name="countrycabin.ist236.Main"
安卓:label="@string/app_name" >
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
主XML
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/LinearLayout1"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="horizontal"
tools:context=".Main" >
<ImageView
安卓:id="@+id/imgCabin"
安卓:layout_width="282dp"
安卓:layout_height="match_parent"
安卓:contentDescription="@string/strCabin"
安卓:src="@drawable/cabin" />
<TableLayout
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent">
<TableRow>
<TextView
安卓:id="@+id/txtTitle"
安卓:layout_width="wrap_content"
安卓:background="#666FFF"
安卓:padding="50dp"
安卓:text="@string/strTitle"
安卓:textSize="40sp"
安卓:typeface="serif" />
</TableRow>
<TableRow>
<TextView
安卓:id="@+id/txtDesc"
安卓:layout_width="wrap_content"
安卓:padding="25dp"
安卓:text="@string/strDesc"
安卓:textSize="30sp" />
</TableRow>
<RadioGroup
安卓:id="@+id/radio"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_weight="0.20"
安卓:contentDescription="@string/strRad" >
<RadioButton
安卓:id="@+id/radCabin1"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginBottom="5dp"
安卓:layout_marginLeft="250dp"
安卓:checked="true"
安卓:text="@string/strCabinOne"
安卓:textSize="25sp" />
<RadioButton
安卓:id="@+id/radCabin2"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginLeft="250dp"
安卓:text="@string/strCabinTwo"
安卓:textSize="25sp" />
</RadioGroup>
<TableRow>
<Button
安卓:id="@+id/btnDate"
安卓:layout_width="50dp"
安卓:padding="20sp"
安卓:text="@string/strDate"
安卓:textSize="25sp" />
</TableRow>
<TableRow>
<Button
安卓:id="@+id/btnTime"
安卓:layout_width="50dp"
安卓:padding="20sp"
安卓:text="@string/strTime"
安卓:textSize="25sp" />
</TableRow>
<TableRow>
<TextView
安卓:id="@+id/txtDate"
安卓:layout_gravity="center"
安卓:padding="20dp"
安卓:textSize="25sp" />
</TableRow>
<TableRow>
<TextView
安卓:id="@+id/txtTime"
安卓:layout_gravity="center"
安卓:padding="20dp"
安卓:textSize="25sp" />
</TableRow>
</TableLayout>
</LinearLayout>
主类(Java代码)
package countrycabin.ist236;
import java.util.Calendar;
import 安卓.os.Bundle;
import 安卓.app.Activity;
import 安卓.app.DatePickerDialog;
import 安卓.app.Dialog;
import 安卓.app.TimePickerDialog;
import 安卓.view.Menu;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.DatePicker;
import 安卓.widget.RadioButton;
import 安卓.widget.TextView;
import 安卓.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;
}
}
# 1 楼答案
检查此代码:现在工作正常
我在XML文件和您的活动中做了一些更改,现在一切正常
但您必须根据您的要求进行一些更改强>
活动: