我正在尝试在我的app中添加数据库。我写了这个类:
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "testDb";
private static final String TABLE_KISILER = "loginCre";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "username";
private static final String KEY_PASS = "password";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Database Oluşturma işlemi.
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_KISILER_TABLE = "CREATE TABLE " + TABLE_KISILER + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_NAME + " VARCHAR(50),"
+ KEY_PASS + " VARCHAR(50)" + ")";
db.execSQL(CREATE_KISILER_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_KISILER);
onCreate(db);
}
//CRUD
// Yeni Kayıt Eklemek.
public void addLoginInfo(String username,String password) {
/*
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, username); // Kisi Adı Getir
values.put(KEY_PASS, password); // Kisi Soyadı Getir
// Ekleme işlemi...
db.insert(TABLE_KISILER, null, values);
db.close(); // Açık olan database i kapat.
*/
}
}
我想从主要活动中访问addLoginInfo
方法,然后我在onCreate
private DatabaseHandler _db;
_db.addLoginInfo("test", "test");
但我的应用程序正在崩溃。有logcat:
12-05 11:24:55.802: E/AndroidRuntime(2841): FATAL EXCEPTION: main
12-05 11:24:55.802: E/AndroidRuntime(2841): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.impact.xxx/com.impact.xxx.MainActivity}: java.lang.NullPointerException
12-05 11:24:55.802: E/AndroidRuntime(2841): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-05 11:24:55.802: E/AndroidRuntime(2841): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-05 11:24:55.802: E/AndroidRuntime(2841): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-05 11:24:55.802: E/AndroidRuntime(2841): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-05 11:24:55.802: E/AndroidRuntime(2841): at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 11:24:55.802: E/AndroidRuntime(2841): at android.os.Looper.loop(Looper.java:137)
12-05 11:24:55.802: E/AndroidRuntime(2841): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-05 11:24:55.802: E/AndroidRuntime(2841): at java.lang.reflect.Method.invokeNative(Native Method)
12-05 11:24:55.802: E/AndroidRuntime(2841): at java.lang.reflect.Method.invoke(Method.java:525)
12-05 11:24:55.802: E/AndroidRuntime(2841): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-05 11:24:55.802: E/AndroidRuntime(2841): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-05 11:24:55.802: E/AndroidRuntime(2841): at dalvik.system.NativeStart.main(Native Method)
12-05 11:24:55.802: E/AndroidRuntime(2841): Caused by: java.lang.NullPointerException
12-05 11:24:55.802: E/AndroidRuntime(2841): at com.impact.xxx.MainActivity.onCreate(MainActivity.java:164)
12-05 11:24:55.802: E/AndroidRuntime(2841): at android.app.Activity.performCreate(Activity.java:5133)
12-05 11:24:55.802: E/AndroidRuntime(2841): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-05 11:24:55.802: E/AndroidRuntime(2841): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-05 11:24:55.802: E/AndroidRuntime(2841): ... 11 more
为什么我收到nullPointerException?
答案 0 :(得分:1)
你没有初始化_db object.try:
private DatabaseHandler _db = new DatabaseHandler(this);
_db.addLoginInfo("test", "test");
答案 1 :(得分:0)
在您的活动中放置这些行,我希望它能够正常工作
DatabaseHandler _db = new DatabaseHandler(this);
_db.getWritableDatabase();
_db.addLoginInfo("test", "test");