NullPointerException崩溃Android应用程序

时间:2014-03-20 11:34:26

标签: java android nullpointerexception

每当我启动活动时,我的应用都会出现NullPointerException错误。这看起来很奇怪,因为一切都被宣布了。

这是我的活动:

package com.example.calendar;

import java.util.Date;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.EditText;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import com.example.calendar.Appointment;


public class CreateAppointment extends Activity implements OnClickListener{

    EditText nameTextBox;
    EditText timeTextBox;
    EditText detailsTextBox;
    Button createButton;
    Date date = new Date();
    Appointment getNewID;

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.create);

        createButton = (Button) findViewById(R.id.apptSave);
        nameTextBox = (EditText)findViewById(R.id.apptName);//Assign the global name box
        timeTextBox = (EditText)findViewById(R.id.apptTime);//Assign the global time box
        detailsTextBox = (EditText)findViewById(R.id.apptDetails);//Assign the global details box


        createButton.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){

        case R.id.apptSave:

            String getNewDate = date.toString();
            String getNewTitle = nameTextBox.getText().toString();
            String getNewTime = timeTextBox.getText().toString();
            String getNewDetails = detailsTextBox.getText().toString();

            DBHandler db = new DBHandler(this);
            db.addAppointment(new Appointment(getNewID.getID(), getNewDate, getNewTitle, getNewTime, getNewDetails));
            finish();
            break;

        }

    }

}

和logcat给了我这个:

03-20 11:24:50.800: E/AndroidRuntime(706): FATAL EXCEPTION: main
03-20 11:24:50.800: E/AndroidRuntime(706): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.calendar/com.example.calendar.CreateAppointment}: java.lang.NullPointerException
03-20 11:24:50.800: E/AndroidRuntime(706): Caused by: java.lang.NullPointerException
03-20 11:24:50.800: E/AndroidRuntime(706):  at com.example.calendar.CreateAppointment.<init>(CreateAppointment.java:25)

出错的行是:

String getNewTitle = nameTextBox.getText().toString();

3 个答案:

答案 0 :(得分:1)

移动

String getNewDate = date.toString();      
String getNewTitle = nameTextBox.getText().toString();
String getNewTime = timeTextBox.getText().toString();
String getNewDetails = detailsTextBox.getText().toString();
onCreate

之后的findViewById

答案 1 :(得分:1)

在onCreate()方法中初始化之前,您正在访问EditText。您需要将以下行移至onCreate()方法

protected void onCreate(Bundle savedInstanceState) 
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.create);

    createButton = (Button) findViewById(R.id.apptSave);
    nameTextBox = (EditText)findViewById(R.id.apptName);//Assign the global name box
    timeTextBox = (EditText)findViewById(R.id.apptTime);//Assign the global time box
    detailsTextBox = (EditText)findViewById(R.id.apptDetails);//Assign the global details box
    createButton.setOnClickListener(this);

    // move lines here 
    String getNewDate = date.toString();
    String getNewTitle = nameTextBox.getText().toString();
    String getNewTime = timeTextBox.getText().toString();
    String getNewDetails = detailsTextBox.getText().toString();
}

答案 2 :(得分:1)

在输入任何值之前,您正在分配编辑文本的值。所以你必须在onCreate方法中移动它。

 String getNewDate = date.toString();
    String getNewTitle = nameTextBox.getText().toString();
    String getNewTime = timeTextBox.getText().toString();
    String getNewDetails = detailsTextBox.getText().toString();

所以你的完整代码看起来像这样,

public class CreateAppointment extends Activity implements OnClickListener{

    EditText nameTextBox;
    EditText timeTextBox;
    EditText detailsTextBox;
    Button createButton;
    Date date = new Date();
    Appointment getNewID;

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.create);

        createButton = (Button) findViewById(R.id.apptSave);
        nameTextBox = (EditText)findViewById(R.id.apptName);//Assign the global name box
        timeTextBox = (EditText)findViewById(R.id.apptTime);//Assign the global time box
        detailsTextBox = (EditText)findViewById(R.id.apptDetails);//Assign the global details box

       String getNewDate = date.toString();
       String getNewTitle = nameTextBox.getText().toString();
       String getNewTime = timeTextBox.getText().toString();
       String getNewDetails = detailsTextBox.getText().toString();

        createButton.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){

        case R.id.apptSave:
            DBHandler db = new DBHandler(this);
            db.addAppointment(new Appointment(getNewID.getID(), getNewDate, getNewTitle, getNewTime, getNewDetails));
            finish();
            break;
        }
    }
}

或者您也可以将此代码放在按钮的点击事件中,例如

@Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch(v.getId()){

            case R.id.apptSave:

       String getNewDate = date.toString();
       String getNewTitle = nameTextBox.getText().toString();
       String getNewTime = timeTextBox.getText().toString();
       String getNewDetails = detailsTextBox.getText().toString();

                DBHandler db = new DBHandler(this);
                db.addAppointment(new Appointment(getNewID.getID(), getNewDate, getNewTitle, getNewTime, getNewDetails));
                finish();
                break;
            }
        }