我正在学习如何制作android应用程序,而我正在开发的应用程序需要一个在启动时填满的数据库,然后只从中读取。
尝试获取sqlitedatabase实例时,在启动时出现错误。
我的数据库管理器构造函数:

public DatabaseManager(Context context)
{
    Log.d("Database Manager", "Database Manager Constructor");
    CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
    db = helper.getReadableDatabase();
    Log.d("Database Manager", db.toString());
    Log.d("Database ", db.getPath());
}

错误在我尝试getReadableDatabase()方法的行中。
这是我的sqlitehelper类的存根,如果它是有用的:
public class CustomSQLiteOpenHelper extends SQLiteOpenHelper
{
    public CustomSQLiteOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        Log.d("Database Manager", "Helper Constructor");
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        Log.v("Database Manager", "In onCreate Method");
        //deleteAllTables(db);
        initializeTables(db);
        fillMonstersTable(db);
    }
}

当我发现错误的时候:
ERROR/AndroidRuntime(366): FATAL EXCEPTION: main
ERROR/AndroidRuntime(366): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{dk.thediabloman.descenthelper/dk.thediabloman.descenthelper.DescentApp}: java.lang.NullPointerException
ERROR/AndroidRuntime(366):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
ERROR/AndroidRuntime(366):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
ERROR/AndroidRuntime(366):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
ERROR/AndroidRuntime(366):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
ERROR/AndroidRuntime(366):     at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(366):     at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(366):     at android.app.ActivityThread.main(ActivityThread.java:4627)
ERROR/AndroidRuntime(366):     at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(366):     at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(366):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
ERROR/AndroidRuntime(366):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
ERROR/AndroidRuntime(366):     at dalvik.system.NativeStart.main(Native Method)
ERROR/AndroidRuntime(366): Caused by: java.lang.NullPointerException
ERROR/AndroidRuntime(366):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
ERROR/AndroidRuntime(366):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
ERROR/AndroidRuntime(366):     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:158)
ERROR/AndroidRuntime(366):     at dk.thediabloman.descenthelper.DatabaseManager.<init>(DatabaseManager.java:52)
ERROR/AndroidRuntime(366):     at dk.thediabloman.descenthelper.DescentApp.<init>(DescentApp.java:18)
ERROR/AndroidRuntime(366):     at java.lang.Class.newInstanceImpl(Native Method)
ERROR/AndroidRuntime(366):     at java.lang.Class.newInstance(Class.java:1429)
ERROR/AndroidRuntime(366):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
ERROR/AndroidRuntime(366):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)

谢谢你的帮助!

最佳答案

这是我的主课
smartdbhelper d=新的smartdbhelper(getapplicationcontext());
d.打开();
//此处的名称、电话和电子邮件已从其他活动中获取
//所以不要盲目地把它放在下面的语句中
插入标题(姓名、电话、电子邮件);
光标c=d.getAllTitles();
//c.getColumnName(0);

  for( int i=0;i<c.getCount();i++){
  if(c.moveToNext()){
      Log.e("c", ""+ c.getString(0) );
      Log.e("d", ""+ c.getString(1) );
      Log.e("d4", ""+ c.getString(2) );


  }
Log.e("r",""+c.getCount());

这是我的sqllite类
公共类smartdbhelper扩展了sqliteopenhelper{
private SQLiteDatabase dBH;
private static final String DATABASE_NAME = "yo.db";
private static final int DATABASE_VERSION = 2;
private static final String NOTIFY_TABLE_NAME = "user_notify_data";
private static final String HR_TABLE_NAME = "user_hr_data";
private static final String NOTIFY_TABLE_CREATE =
    "CREATE TABLE " + NOTIFY_TABLE_NAME + " (counter INTEGER PRIMARY KEY, " +
    "userresponse TEXT, " +
    "notifytime TEXT);";


private static final String HR_TABLE_CREATE =
    "CREATE TABLE " + HR_TABLE_NAME +
    " (counter INTEGER PRIMARY KEY, " +
    "hr TEXT, " +
    "act TEXT, " +
    "timestamp TEXT);";

public SmartDBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
    }

@Override
public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        Log.e("smartdbhelper", "before creation");
        db.execSQL(NOTIFY_TABLE_CREATE);
        Log.e("smartdbhelper", "middle creation");
        db.execSQL(HR_TABLE_CREATE);
        Log.e("smartdbhelper", "after creation");
        }

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

    public SmartDBHelper open() throws SQLException
    {
          dBH = getWritableDatabase();
         return this;
    }

    public long insertTitle(String isbn, String title, String publisher)
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put("hr", isbn);
        initialValues.put("act", title);
        initialValues.put("timestamp", publisher);
        Log.e("insertes","i");

         dBH.insert(HR_TABLE_NAME, null, initialValues);

返回1111111;
}
    public Cursor getAllTitles()
    {
        return dBH.query(HR_TABLE_NAME, new String[] {
                 "hr","act","timestamp"},null,
                null,
                null,
                null,
                null);
    }








    //---closes the database---
    public void close()
    {
        close();

}
}
这很有效。我试过了

09-17 22:48