1

我正在尝试实现一个应用程序,其中在 edittext 中输入的文本保存到数据库中,然后显示在文本字段中。但是当我尝试这样做时应用程序崩溃并且我得到一个空指针异常。这是我的代码

   package com.example.dbex;

import java.sql.SQLOutput;
import java.sql.SQLPermission;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHandler extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION=1;
    public static final String DATABASE_NAME="contactsmanager";
    public static final String TABLE_CONTACTS="contacts";
    public static final String KEY_ID="id";
    public static final String KEY_NAME="name";
    public static final String KEY_NUMBER="number";
    public DBHandler(Context context) {
        super(context,DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String CREATE_TABLE_CONTACTS="CREATE TABLE "+TABLE_CONTACTS+"("+KEY_ID+" INTEGER PRIMARY KEY,"+KEY_NAME+" TEXT,"+KEY_NUMBER+" TEXT"+")";
        db.execSQL(CREATE_TABLE_CONTACTS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_CONTACTS);
        onCreate(db);
    }

    public void addContact(Contacts contacts)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(KEY_NAME, contacts.getname());
        values.put(KEY_NUMBER, contacts.getnumber());
        db.insert(TABLE_CONTACTS,null,values);
        db.close();
    }

    public Contacts getContact(int id)
    {
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor c=db.query(TABLE_CONTACTS, new String[]{KEY_ID,KEY_NAME,KEY_NUMBER},KEY_ID+" =?",new String[]{String.valueOf(id)}, null, null, null, null);
        Contacts contacts=null;
        if(c!=null&&c.moveToFirst())
        {
            contacts=new Contacts(Integer.parseInt(c.getString(0)),c.getString(1),c.getString(2));
        }
        return contacts;
    }

    public void deleteContact(Contacts contacts)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        db.delete(TABLE_CONTACTS, KEY_ID+" =?", new String[]{String.valueOf(contacts.getID())});
        db.close();
    }


}

主要活动是

    package com.example.dbex;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b1=(Button)findViewById(R.id.button1);
        EditText et1=(EditText)findViewById(R.id.editText1);
        EditText et2=(EditText)findViewById(R.id.editText2);
        final TextView tv1=(TextView)findViewById(R.id.textView1);
        final String _name=et1.getText().toString();
        final String _phone=et2.getText().toString();
        final DBHandler dbh=new DBHandler(getBaseContext());
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dbh.addContact(new Contacts(_name, _phone));
                String contact1=(dbh.getContact(0)).getname();
                tv1.setText(contact1);
            }
        });

    }

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

}

我在该行中得到空指针异常

                String contact1=(dbh.getContact(0)).getname();

我的错误在哪里?谢谢

4

1 回答 1

0

由于您告诉您可以插入该值,因此我假设您的数据库创建是正确的您已将要查询的 id 硬编码为 0。您可能在这里没有任何价值。请参阅这篇文章以了解即使您删除现有行,自动增量 ID 也可能从上次使用的值而不是 0 继续。因此您可能会在这部分代码中返回空值:

Contacts contacts=null;
        if(c!=null&&c.moveToFirst())
        {
            contacts=new Contacts(Integer.parseInt(c.getString(0)),c.getString(1),c.getString(2));
        }
        return contacts;
于 2013-06-29T11:41:04.480 回答