I have no errors on the code, but when I launch I get a fatal error in my main XML page that says,
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
I'm not sure what is causing the problem. As I said, I have no errors. I made a program very similar to this that worked fine, I just added the TimePicker to this one.
What I'm trying to do is pretty straightforward. I'm making the program launch, and when I click a button, a dialog displays that shows either a DatePicker or TimePicker, and when someone selects a date words display in a TextView at the bottom. I'm unable to test even this, however, as it will not even launch. My emulator tells me it will not respond.
I'm using Honeycomb for this, as stated in a textbook.
Here is all my code.
Main Manifest XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="countrycabin.ist236"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="11" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="countrycabin.ist236.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
main XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".Main" >
<ImageView
android:id="@+id/imgCabin"
android:layout_width="282dp"
android:layout_height="match_parent"
android:contentDescription="@string/strCabin"
android:src="@drawable/cabin" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:background="#666FFF"
android:padding="50dp"
android:text="@string/strTitle"
android:textSize="40sp"
android:typeface="serif" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/txtDesc"
android:layout_width="wrap_content"
android:padding="25dp"
android:text="@string/strDesc"
android:textSize="30sp" />
</TableRow>
<RadioGroup
android:id="@+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:contentDescription="@string/strRad" >
<RadioButton
android:id="@+id/radCabin1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="250dp"
android:checked="true"
android:text="@string/strCabinOne"
android:textSize="25sp" />
<RadioButton
android:id="@+id/radCabin2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="250dp"
android:text="@string/strCabinTwo"
android:textSize="25sp" />
</RadioGroup>
<TableRow>
<Button
android:id="@+id/btnDate"
android:layout_width="50dp"
android:padding="20sp"
android:text="@string/strDate"
android:textSize="25sp" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnTime"
android:layout_width="50dp"
android:padding="20sp"
android:text="@string/strTime"
android:textSize="25sp" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/txtDate"
android:layout_gravity="center"
android:padding="20dp"
android:textSize="25sp" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/txtTime"
android:layout_gravity="center"
android:padding="20dp"
android:textSize="25sp" />
</TableRow>
</TableLayout>
</LinearLayout>
Main class (Java code)
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;
}
}
Check this Code: Its working fine now.

I have made some change in XML file as well as in Your Activity now its working fine
But You have to made some changes depend on your requirement.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgCabin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="strCabin"
android:src="@drawable/ic_launcher" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#666FFF"
android:text="strTitle"
android:textSize="40sp"
android:typeface="serif" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/txtDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="strDesc"
/>
</TableRow>
<TableRow>
<RadioGroup
android:id="@+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="strRad" >
<RadioButton
android:id="@+id/radCabin1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="250dp"
android:checked="true"
android:text="strCabinOne"
android:textSize="25sp" />
<RadioButton
android:id="@+id/radCabin2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="250dp"
android:text="strCabinTwo"
android:textSize="25sp" />
</RadioGroup>
</TableRow>
<TableRow>
<Button
android:id="@+id/btnDate"
android:layout_width="50dp"
android:padding="20sp"
android:text="strDate"
android:textSize="25sp" />
</TableRow>
<TableRow>
<Button
android:id="@+id/btnTime"
android:layout_width="50dp"
android:padding="20sp"
android:text="strTime"
android:textSize="25sp" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/txtDate"
android:layout_gravity="center"
android:padding="20dp"
android:textSize="25sp" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/txtTime"
android:layout_gravity="center"
android:padding="20dp"
android:textSize="25sp" />
</TableRow>
</TableLayout>
</LinearLayout>
Activity :
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;
}
}
来源:https://stackoverflow.com/questions/15561112/using-datepicker-and-timepicker-dialog-boxes-android-java-in-eclipse