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

标签 java android eclipse datepicker timepicker

我的代码没有错误,但是当我启动时,我在主 XML 页面中收到 fatal error ,内容为:

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

我不确定是什么原因导致了这个问题。正如我所说,我没有错误。我制作了一个与此非常相似的程序,运行良好,我只是将 TimePicker 添加到此程序中。

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

正如教科书中所述,我正在使用 Honeycomb 来实现此目的。

这是我的所有代码。

主要 list 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;
}

 }

最佳答案

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

enter image description here

我对 XML 文件以及您的 Activity 进行了一些更改,现在工作正常

但是您必须根据您的要求进行一些更改。

<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;
    }

}

关于java - 使用 DatePicker 和 TimePicker 对话框 - Eclipse 中的 Android Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15561112/

相关文章:

java - 保持连接到没有互联网的网络 - Android

java - 为什么下面的按位运算会产生数值溢出?

java - 从 Java 编写 excel 文件的最佳方法是什么?

java - 通过 JNI 从 Applet 调用 DLL

android - 如何在AndEngine中制作计时器?

c++ - 无法找到 -libboost_system

java - 我如何获得数组来保存所玩游戏的数量并为每个玩家运行它?

linux - 在 Linux 上的 IDE 下没有声音(在 Jar 中正常)

java - 如何使用java在链表中插入节点?

java - OQL 查找实现 java.security.Principal 的实例