应用程序关闭插入查询SQLite

时间:2014-12-24 05:53:24

标签: java sqlite

当我插入此示例记录进行测试时,我的应用程序关闭,我在很多方面尝试了它。从活动类im发送字符串值以插入数据库。

我的MainInvoiceActivity Class

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)

1 个答案:

答案 0 :(得分:1)

正确的代码将如下所示

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(); 
    }
}