我尝试连接到数据库表时出现运行时错误

时间:2013-08-02 09:31:50

标签: android

我的应用使用SQLiteOpenHelper。 表已成功创建,但SQL lite管理器显示空表。

public class DBHelper extends SQLiteOpenHelper {
   public static final String DATABASE_NAME = "MyBD.db";
   public static final int DATABASE_VERSION = 1;

   public final String SMS_TABLE = "sms_table";

public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }   

@Override
public void onCreate(SQLiteDatabase db) { 
try
{
    String request;
    request ="create table " + SMS_TABLE + "("
            + "id INTEGER primary key autoincrement," 
            + "sender text,"
            + "date text,"
            + "read text,"
            + "message text" + ");";        
    db.execSQL(request);
}
catch(SQLException ex)
{
    Log.d("DBCreator", "onCreate exception " +ex.getMessage()); 
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}

使用它......

public class SecureMessagesActivity extends Activity
{
DBHelper dbHelper;
public void onCreate(Bundle savedInstanceState) 
{
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     SQLiteDatabase db = dbHelper.getWritableDatabase();

     db.close();
}

结果看起来很伤心......(logcat)

08-02 09:25:42.976: D/AndroidRuntime(29643): Shutting down VM
08-02 09:25:42.976: W/dalvikvm(29643): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-02 09:25:42.996: E/AndroidRuntime(29643): FATAL EXCEPTION: main
08-02 09:25:42.996: E/AndroidRuntime(29643): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.secure.sms/org.secure.sms.SecureMessagesActivity}: java.lang.NullPointerException
08-02 09:25:42.996: E/AndroidRuntime(29643):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-02 09:25:42.996: E/AndroidRuntime(29643):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-02 09:25:42.996: E/AndroidRuntime(29643):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-02 09:25:42.996: E/AndroidRuntime(29643):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-02 09:25:42.996: E/AndroidRuntime(29643):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 09:25:42.996: E/AndroidRuntime(29643):    at android.os.Looper.loop(Looper.java:123)
08-02 09:25:42.996: E/AndroidRuntime(29643):    at android.app.ActivityThread.main(ActivityThread.java:3683)
08-02 09:25:42.996: E/AndroidRuntime(29643):    at java.lang.reflect.Method.invokeNative(Native Method)
08-02 09:25:42.996: E/AndroidRuntime(29643):    at java.lang.reflect.Method.invoke(Method.java:507)
08-02 09:25:42.996: E/AndroidRuntime(29643):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-02 09:25:42.996: E/AndroidRuntime(29643):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-02 09:25:42.996: E/AndroidRuntime(29643):    at dalvik.system.NativeStart.main(Native Method)
08-02 09:25:42.996: E/AndroidRuntime(29643): Caused by: java.lang.NullPointerException
08-02 09:25:42.996: E/AndroidRuntime(29643):    at org.secure.sms.SecureMessagesActivity.onCreate(SecureMessagesActivity.java:40)
08-02 09:25:42.996: E/AndroidRuntime(29643):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-02 09:25:42.996: E/AndroidRuntime(29643):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-02 09:25:42.996: E/AndroidRuntime(29643):    ... 11 more
08-02 09:25:43.027: W/ActivityManager(70):   Force finishing activity org.secure.sms/.SecureMessagesActivity
08-02 09:25:43.547: W/ActivityManager(70): Activity pause timeout for HistoryRecord{40727540 org.secure.sms/.SecureMessagesActivity}
08-02 09:25:45.227: W/InputManagerService(70): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40606e60
08-02 09:25:45.267: I/Process(29643): Sending signal. PID: 29643 SIG: 9
08-02 09:25:45.297: I/ActivityManager(70): Process org.secure.sms (pid 29643) has died.
08-02 09:25:50.508: D/dalvikvm(280): GC_EXPLICIT freed 15K, 55% free 2599K/5703K, external 716K/1038K, paused 71ms
08-02 09:25:53.987: W/ActivityManager(70): Activity destroy timeout for HistoryRecord{40727540 org.secure.sms/.SecureMessagesActivity}
08-02 09:25:55.607: D/dalvikvm(315): GC_EXPLICIT freed 4K, 54% free 2539K/5511K, external 716K/1038K, paused 95ms

4 个答案:

答案 0 :(得分:2)

你在哪里实例化dbHelper?您的dbHelpernull

SQLiteDatabase db = dbHelper.getWritableDatabase();

答案 1 :(得分:0)

java.lang.NullPointerException

DBHelper dbHelper;中的null SecureMessagesActivity

所以使用

public class SecureMessagesActivity extends Activity
{
DBHelper dbHelper;
public void onCreate(Bundle savedInstanceState) 
{
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     dbHelper=new DBHelper(this);// pass context 
     SQLiteDatabase db = dbHelper.getWritableDatabase();

     db.close();
}

答案 2 :(得分:0)

您尚未初始化您的databaseHelper:

   public class SecureMessagesActivity extends Activity
   {
 DBHelper dbHelper; //<-- no init
     public void onCreate(Bundle savedInstanceState) 
     {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 dbHelper = new DBHelper(this); //<-- do this

 SQLiteDatabase db = dbHelper.getWritableDatabase();

 db.close();
    }

答案 3 :(得分:-1)

/查询为配置文件和步骤创建表     private static final String CREATE_PROFILE_TABLE =“create table profile(_id integer not null primary key autoincrement,profile text null,status text not null)”;

private static final String CREATE_STEP_TABLE = "create table step (_id integer not null primary key autoincrement,value integer not null,delay integer not null,profile_id integer default 0, FOREIGN KEY (profile_id) REFERENCES profile(id))";


//constructor
public EcConferenceOpenHelper(Context context, String name,
        CursorFactory factory, int version) {
    super(context, name, factory, version);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

    db.execSQL(CREATE_PROFILE_TABLE);
    db.execSQL(CREATE_STEP_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

    onCreate(db);
}