我正在尝试制作使用SQLite database的简单注册/登录应用程序,到目前为止,我已经获得了此代码。这是我的“注册”活动,我想按注册按钮后应该将用户名和pin(密码)实现到数据库,可惜该应用程序崩溃了。

  EditText reName, rePin;
    Button reRegister;
    DatabaseHelper helper = new DatabaseHelper(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);

        reName = (EditText)findViewById(R.id.reName);
        rePin = (EditText)findViewById(R.id.rePin);
        reRegister = (Button)findViewById(R.id.reRegister);
        reRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String Imie = reName.getText().toString();
                String Haslo = rePin.getText().toString();

                Contacts c =new Contacts();
                c.setName(Imie);
                c.setPass(Haslo);

                helper.insertContacts(c);


                Intent intent = new Intent(SignUp.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }


这也是我的DatabaseHelper代码,我想问题出在代码中,但是我找不到。我该怎么办或应该在哪里找到解决方案? :)

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "contacts.db";
    private static final String TABLE_NAME = "contacts";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_PASS = "pass";
    SQLiteDatabase db;

    private static final String TABLE_CREATE = "create table contacts (id integer primary key not null , " +
            "name text not null, pass text not null);";

    public DatabaseHelper(Context context){
        super(context, DATABASE_NAME, null, 1);

    }

    public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        db.execSQL(TABLE_CREATE);
        this.db = db;

    }

    public void insertContacts(Contacts c){
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        String query = "select + from contacts";
        Cursor cursor = db.rawQuery(query, null);
        int count = cursor.getCount();
        values.put(COLUMN_ID, count);
        values.put(COLUMN_NAME, c.getName());
        values.put(COLUMN_PASS, c.getPass());
        db.insert(TABLE_NAME, null, values);
        db.close();
    }

    public String searchPass(String name) {
        db = this.getReadableDatabase();
        String query = "select * from "+TABLE_NAME;
        Cursor cursor = db.rawQuery(query, null);
        String a,b;
        b = "not found";
        if (cursor.moveToFirst()){
            do {
                a =cursor.getString(2);
                if (a.equals(name)){
                    b= cursor.getString(3);
                    break;
                }

            }
            while (cursor.moveToNext());
        }
        return b;
    }


    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        String query = "DROP TABLE IF EXISTS" + TABLE_NAME;
        db.execSQL(query);
        this.onCreate(db);

    }
}


@edit-Logcat错误

12-16 13:08:14.316 3541-3541/com.example.mateusz.sqllogowanie E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.example.mateusz.sqllogowanie, PID: 3541
                                                                                java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String)' on a null object reference
                                                                                    at com.example.mateusz.sqllogowanie.DatabaseHelper.onCreate(DatabaseHelper.java:36)
                                                                                    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:333)
                                                                                    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:238)
                                                                                    at com.example.mateusz.sqllogowanie.DatabaseHelper.insertContacts(DatabaseHelper.java:42)
                                                                                    at com.example.mateusz.sqllogowanie.SignUp$1.onClick(SignUp.java:36)
                                                                                    at android.view.View.performClick(View.java:6294)
                                                                                    at android.view.View$PerformClick.run(View.java:24770)
                                                                                    at android.os.Handler.handleCallback(Handler.java:790)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                    at android.os.Looper.loop(Looper.java:164)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

最佳答案

您的SqliteDatabase对象为null。

更改此:

  @Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    db.execSQL(TABLE_CREATE); //db is null here
    this.db = db;
}


至:

  @Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    this.db = sqLiteDatabase;
    db.execSQL(TABLE_CREATE);
}


同样在您的insertContacts()方法中,将查询更改为:

String query = "select * from contacts";

09-07 15:19