我的应用程序使用SQLiteOpenHelper
表已成功创建,但SQL lite Manager显示空表。

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

最佳答案

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

SQLiteDatabase db = dbHelper.getWritableDatabase();

关于android - 尝试连接数据库表时出现运行时错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18013444/

10-10 23:53