Использование диалоговых окон DatePicker и TimePicker - Android Java в Eclipse

У меня нет ошибок в коде, но при запуске я получаю фатальную ошибку на главной странице 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 внизу. Однако я не могу проверить даже это, поскольку он даже не запустится. Мой эмулятор говорит мне, что он не будет отвечать.

Я использую Соты для этого, как указано в учебнике.

Вот весь мой код.

Основной Манифест 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>

основной 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>

Основной класс (код 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;
}

 }

Ответы на вопрос(1)

Ваш ответ на вопрос