Java 应用程序在插入查询SQLlite时关闭

Java 应用程序在插入查询SQLlite时关闭,java,sqlite,Java,Sqlite,当我插入样本记录进行测试时,我的应用程序关闭,我尝试了很多方法。从活动类im发送要插入到数据库的字符串值 我的主发票活动类 public class MainInvoiceActivity extends Activity { private DBHelper mydb ; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView

当我插入样本记录进行测试时,我的应用程序关闭,我尝试了很多方法。从活动类im发送要插入到数据库的字符串值

我的主发票活动类

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(); 
}
}
}
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;
   }
}
我的数据库SQLiteOpenHelper类

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

显示您的logcat请让上面的logcat消息显示您的活动类别代码。还要告诉MainInvoiceActivity.java的第67行是哪段代码,问题是您从未初始化过mydb实例。在使用它之前,您应该使用它。我已初始化。。sry我在代码中添加了…thnks,我错过了
mydb=new DBHelper(this)
然后出现了新的错误。LOG
12-24 02:04:32.896:E/SQLiteLog(1234):(1)没有这样的表:示例