0

我的代码没有错误,但是当我启动时,我的主要 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 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;
}

 }
4

1 回答 1

1

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

在此处输入图像描述

我已经对 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="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>

活动 :

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

}
于 2013-03-22T06:30:55.297 回答