gpt4 book ai didi

java - 访问内部类方法会导致 nullpointerException

转载 作者:行者123 更新时间:2023-12-01 12:10:20 25 4
gpt4 key购买 nike

我正在尝试在我的应用程序中添加数据库。我编写了此类:

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.
*/
}

}

我想从主 Activity 访问 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 ?

最佳答案

您没有初始化 _db 对象。试试这个:

private DatabaseHandler _db = new DatabaseHandler(this);
_db.addLoginInfo("test", "test");

关于java - 访问内部类方法会导致 nullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27315297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com