sqlite数据库无法正常工作错误638

时间:2014-03-17 15:52:03

标签: java android sqlite logcat fatal-error

每当我想在android上的数据库列中添加一行时,它就会给我一个致命错误,而LogCat并没有告诉我导致问题的原因。它只是告诉我文件和PID,这也告诉我这是一个致命错误,PID号是638。

基本上,我需要一个Dialog来显示用户需要填写的输入字段,以便将约会添加到数据库。

代码:

    package com.example.calendar;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CalendarView;
import android.widget.EditText;
import android.app.Activity;
import android.content.Intent;
import static android.provider.BaseColumns._ID;
import static com.example.calendar.Constants.TABLE_NAME;
import static com.example.calendar.Constants.TIME;
import static com.example.calendar.Constants.TITLE;
import static com.example.calendar.Constants.DETAILS;
import static com.example.calendar.Constants.DATE;
import static com.example.calendar.Constants.CONTENT_URI;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class CreateAppointment extends Activity implements OnClickListener{

    private static String[] FROM = { _ID, DATE, TIME, TITLE, DETAILS};
    private static String ORDER_BY = TIME + " ASC";
    private AppointmentsData appointments;
    CalendarView calendar;
    String string;
    EditText nameTextBox;
    EditText timeTextBox;
    EditText detailsTextBox;

    protected void onCreate(Bundle savedInstanceState) {

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

        View createButton = findViewById(R.id.apptSave);
        View nameTextBox = (EditText)findViewById(R.id.apptName);
        View timeTextBox = (EditText)findViewById(R.id.apptTime);
        View detailsTextBox = (EditText)findViewById(R.id.apptDetails);
        calendar = (CalendarView)findViewById(R.id.calendar);
        createButton.setOnClickListener(this);

    }

    private void addAppointment(String string) {
        /* Insert a new record into the Events data
        source. You would do something similar
        for delete and update. */
        String getTitle = nameTextBox.toString();
        String getTime = timeTextBox.toString();
        String getDetails = detailsTextBox.toString();

        SQLiteDatabase db = appointments.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DATE, calendar.getDate());
        values.put(TIME, getTime);
        values.put(TITLE, getTitle);
        values.put(DETAILS, getDetails);
        getContentResolver().insert(CONTENT_URI, values);
        }

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

        case R.id.apptSave:
            addAppointment(string);
            finish();
            break;

        }

    }

}

堆栈跟踪:

    03-17 15:32:21.547: E/AndroidRuntime(638): FATAL EXCEPTION: main
03-17 15:32:21.547: E/AndroidRuntime(638): java.lang.NullPointerException
03-17 15:32:21.547: E/AndroidRuntime(638):  at com.example.calendar.CreateAppointment.addAppointment(CreateAppointment.java:51)
03-17 15:32:21.547: E/AndroidRuntime(638):  at com.example.calendar.CreateAppointment.onClick(CreateAppointment.java:70)
03-17 15:32:21.547: E/AndroidRuntime(638):  at android.view.View.performClick(View.java:3480)
03-17 15:32:21.547: E/AndroidRuntime(638):  at android.view.View$PerformClick.run(View.java:13983)
03-17 15:32:21.547: E/AndroidRuntime(638):  at android.os.Handler.handleCallback(Handler.java:605)
03-17 15:32:21.547: E/AndroidRuntime(638):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-17 15:32:21.547: E/AndroidRuntime(638):  at android.os.Looper.loop(Looper.java:137)
03-17 15:32:21.547: E/AndroidRuntime(638):  at android.app.ActivityThread.main(ActivityThread.java:4340)
03-17 15:32:21.547: E/AndroidRuntime(638):  at java.lang.reflect.Method.invokeNative(Native Method)
03-17 15:32:21.547: E/AndroidRuntime(638):  at java.lang.reflect.Method.invoke(Method.java:511)
03-17 15:32:21.547: E/AndroidRuntime(638):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-17 15:32:21.547: E/AndroidRuntime(638):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-17 15:32:21.547: E/AndroidRuntime(638):  at dalvik.system.NativeStart.main(Native Method)

请帮忙。

1 个答案:

答案 0 :(得分:0)

您已在onCreate中重新声明变量,使其成为onCreate的本地变量,而实例变量未初始化。

因此NPE @ CreateAppointment.java第51行可能是String getTitle = nameTextBox.toString();

更改为

EditText nameTextBox;
EditText timeTextBox;
EditText detailsTextBox;
Button createButton ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
createButton = (Button)findViewById(R.id.apptSave);
nameTextBox = (EditText)findViewById(R.id.apptName);
timeTextBox = (EditText)findViewById(R.id.apptTime);
detailsTextBox = (EditText)findViewById(R.id.apptDetails);
calendar= (CalendarView)findViewById(R.id.calendar);

并改变这个

String getTitle = nameTextBox.toString();
String getTime = timeTextBox.toString();
String getDetails = detailsTextBox.toString();

String getTitle = nameTextBox.getText().toString();
String getTime = timeTextBox.getText().toString();
String getDetails = detailsTextBox.getText().toString();

编辑:

见这个

private AppointmentsData appointments;
String string;

仅声明未初始化。

  1. 您尚未初始化string

  2. 您尚未初始化appointments

     appointments = new AppointmentsData(this); // after activity is created
     // can be initialized in onCreate
    
  3. 因此你获得了NPE