1

を使用する私のアプリ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

4 に答える 4

2

どこでインスタンス化しましたdbHelperか? あなたdbHelpernull

SQLiteDatabase db = dbHelper.getWritableDatabase();
于 2013-08-02T09:35:19.833 に答える
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();
}
于 2013-08-02T09:36:03.853 に答える
0

データベースヘルパーを初期化していません:

   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();
    }
于 2013-08-02T09:36:03.870 に答える
-1

プロファイルおよびステップ用のテーブルを作成するための /query private static final String CREATE_PROFILE_TABLE = "テーブル プロファイルを作成します(_id integer not null 主キー 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);
}
于 2013-08-02T09:52:44.833 に答える