gpt4 book ai didi

java - 应用程序在插入查询 SQLite 时关闭

转载 作者:行者123 更新时间:2023-11-30 11:10:39 26 4
gpt4 key购买 nike

当我插入此示例记录进行测试时,我的应用程序关闭,我尝试了很多方法。从 Activity 类即时发送字符串值以插入到数据库中。

我的 MainInvoiceActivity 类

public class MainInvoiceActivity extends Activity {
private DBHelper mydb ;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_invoice);

if ( mydb.insertsample( "one" , "two" , "3" )){ // LINE NO 67
Toast.makeText(getApplicationContext(), "Insert Success!", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();
}
}
}

我的数据库SQLiteOpenHelper类

public class DBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "MyDBName.db";
public static final String TABLE_SAMPLE = "sample";

public DBHelper(Context context)
{
super(context, DATABASE_NAME , null, 1);
}
public void onCreate(SQLiteDatabase db) {

String CREATE_SAMPLE_TABLE = "CREATE TABLE sample ( " +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"one TEXT, "+
"two TEXT, "+
"three TEXT )";
db.execSQL(CREATE_SAMPLE_TABLE);
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS sample");
onCreate(db);
}
public boolean insertsample (String one, String two, String three)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues myValues = new ContentValues();

myValues.put(one, one);
myValues.put(two, two);
myValues.put(three, three);

db.insert(TABLE_SAMPLE, null, myValues);
return true;
}
}

日志消息

E/AndroidRuntime(1153): FATAL EXCEPTION: main
E/AndroidRuntime(1153): Process: com.ezycode.pos, PID: 1153
E/AndroidRuntime(1153): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ezycode.pos/com.ezycode.pos.MainInvoiceActivity}: java.lang.NullPointerException
E/AndroidRuntime(1153): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
E/AndroidRuntime(1153): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
E/AndroidRuntime(1153): at android.app.ActivityThread.access$800(ActivityThread.java:135)
E/AndroidRuntime(1153): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
E/AndroidRuntime(1153): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(1153): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(1153): at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime(1153): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1153): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(1153): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime(1153): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E/AndroidRuntime(1153): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(1153): Caused by: java.lang.NullPointerException
E/AndroidRuntime(1153): at com.ezycode.pos.MainInvoiceActivity.onCreate(MainInvoiceActivity.java:67)
E/AndroidRuntime(1153): at android.app.Activity.performCreate(Activity.java:5231)
E/AndroidRuntime(1153): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime(1153): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

最佳答案

正确的代码如下所示

public class MainInvoiceActivity extends Activity {
private DBHelper mydb ;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_invoice);

mydb = new DBHelper(this); //This is missing your code.

if ( mydb.insertsample( "one" , "two" , "3" )){ // LINE NO 67
Toast.makeText(getApplicationContext(), "Insert Success!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();
}
}

关于java - 应用程序在插入查询 SQLite 时关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27632162/

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