-1

这是数据库类。它在 getReadableDatabase() 中显示错误。下面是日志文件。请帮我看看错在哪里。

 package achira.test;

  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;

 public class DatabaseHelper extends SQLiteOpenHelper {

static final String dbName="achira";
static final String employeeTable="patient";
static final String colID="UID";
static final String colName="patientName";
static final String colAge="Age";
static final String colDept="Test";

static final String deptTable="Test1";
static final String colDeptID="TestID";
static final String colDeptName="TesttName";

static final String viewEmps="ViewPatient";


public DatabaseHelper(Context context) {
    super(context, dbName, null,40);

    // TODO Auto-generated constructor stub
}

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

    db.execSQL("CREATE TABLE "+deptTable+" ("+colDeptID+ " INTEGER PRIMARY KEY , "+
            colDeptName+ " TEXT)");

    db.execSQL("CREATE TABLE "+employeeTable+" ("+colID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
            colName+" TEXT, "+colAge+" Integer, "+colDept+" INTEGER NOT NULL ,FOREIGN KEY ("+colDept+") REFERENCES "+deptTable+" ("+colDeptID+"));");


    db.execSQL("CREATE TRIGGER fk_empdept_deptid " +
            " BEFORE INSERT "+
            " ON "+employeeTable+

            " FOR EACH ROW BEGIN"+
            " SELECT CASE WHEN ((SELECT "+colDeptID+" FROM "+deptTable+" WHERE "+colDeptID+"=new."+colDept+" ) IS NULL)"+
            " THEN RAISE (ABORT,'Foreign Key Violation') END;"+
            "  END;");

    db.execSQL("CREATE VIEW "+viewEmps+
            " AS SELECT "+employeeTable+"."+colID+" AS _id,"+
            " "+employeeTable+"."+colName+","+
            " "+employeeTable+"."+colAge+","+
            " "+deptTable+"."+colDeptName+""+
            " FROM "+employeeTable+" JOIN "+deptTable+
            " ON "+employeeTable+"."+colDept+" ="+deptTable+"."+colDeptID
            );
    //Inserts pre-defined departments
    InsertDepts(db);

}

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

    db.execSQL("DROP TABLE IF EXISTS "+employeeTable);
    db.execSQL("DROP TABLE IF EXISTS "+deptTable);

    db.execSQL("DROP TRIGGER IF EXISTS dept_id_trigger");
    db.execSQL("DROP TRIGGER IF EXISTS dept_id_trigger22");
    db.execSQL("DROP TRIGGER IF EXISTS fk_empdept_deptid");
    db.execSQL("DROP VIEW IF EXISTS "+viewEmps);
    onCreate(db);
}

 void patient(patient emp)
{


     SQLiteDatabase db= this.getWritableDatabase();


    ContentValues cv=new ContentValues();

    cv.put(colName, emp.getName());
    cv.put(colAge, emp.getAge());
    cv.put(colDept, emp.getDept());
    //cv.put(colDept,2);

    db.insert(employeeTable, colName, cv);
    db.close();


}

 int getEmployeeCount()
 {
    SQLiteDatabase db=this.getWritableDatabase();
    Cursor cur= db.rawQuery("Select * from "+employeeTable, null);
    int x= cur.getCount();
    cur.close();
    return x;
 }

 Cursor getAllEmployees()
 {
     SQLiteDatabase db=this.getWritableDatabase();



     //Cursor cur= db.rawQuery("Select "+colID+" as _id , "+colName+", "+colAge+" from "+employeeTable, new String [] {});
     Cursor cur= db.rawQuery("SELECT * FROM "+viewEmps,null);
     return cur;

 }

 Cursor getAllDepts()
 {
     SQLiteDatabase db=this.getReadableDatabase();
     Cursor cur=db.rawQuery("SELECT "+colDeptID+" as _id, "+colDeptName+" from "+deptTable,new String [] {});

     return cur;
 }

 void InsertDepts(SQLiteDatabase db)
 {
     ContentValues cv=new ContentValues();
        cv.put(colDeptID, 1);
        cv.put(colDeptName, "XYZ");
        db.insert(deptTable, colDeptID, cv);
        cv.put(colDeptID, 2);
        cv.put(colDeptName, "ABC");
        db.insert(deptTable, colDeptID, cv);
        cv.put(colDeptID, 3);
        cv.put(colDeptName, "HIV");
        db.insert(deptTable, colDeptID, cv);
        cv.put(colDeptID, 4);
        cv.put(colDeptName, "HIV");
        db.insert(deptTable, colDeptID, cv);
        db.insert(deptTable, colDeptID, cv);

 }

 public String GetDept(int ID)
 {
     SQLiteDatabase db=this.getReadableDatabase();

     String[] params=new String[]{String.valueOf(ID)};
     Cursor c=db.rawQuery("SELECT "+colDeptName+" FROM"+ deptTable+" WHERE "+colDeptID+"=?",params);
     c.moveToFirst();
     int index= c.getColumnIndex(colDeptName);
     return c.getString(index);
 }

 public Cursor getEmpByDept(String Dept)
 {
     SQLiteDatabase db=this.getReadableDatabase();
     String [] columns=new String[]{"_id",colName,colAge,colDeptName};
     Cursor c=db.query(viewEmps, columns, colDeptName+"=?", new String[]{Dept}, null, null, null);
     return c;
 }

 public int GetDeptID(String Dept)
 {
     SQLiteDatabase db=this.getReadableDatabase();
     Cursor c=db.query(deptTable, new String[]{colDeptID+" as _id",colDeptName},colDeptName+"=?", new String[]{Dept}, null, null, null);
     //Cursor c=db.rawQuery("SELECT "+colDeptID+" as _id FROM "+deptTable+" WHERE "+colDeptName+"=?", new String []{Dept});
     c.moveToFirst();
     return c.getInt(c.getColumnIndex("_id"));

     }

 public int UpdateEmp(patient emp)
 {
     SQLiteDatabase db=this.getWritableDatabase();
     ContentValues cv=new ContentValues();
     cv.put(colName, emp.getName());
     cv.put(colAge, emp.getAge());
     cv.put(colDept, emp.getDept());
     return db.update(employeeTable, cv, colID+"=?", new String []{String.valueOf(emp.getID())});

 }

 public void DeleteEmp(patient emp)
 {
     SQLiteDatabase db=this.getWritableDatabase();
     db.delete(employeeTable,colID+"=?", new String [] {String.valueOf(emp.getID())});
     db.close();



 }

}

这是日志文件:

06-19 13:17:32.854: W/dalvikvm(873): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-19 13:17:32.934: E/AndroidRuntime(873): FATAL EXCEPTION: main
06-19 13:17:32.934: E/AndroidRuntime(873): java.lang.RuntimeException: Unable to start activity ComponentInfo{achira.test/achira.test.GridList}: java.lang.NullPointerException
06-19 13:17:32.934: E/AndroidRuntime(873):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-19 13:17:32.934: E/AndroidRuntime(873):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-19 13:17:32.934: E/AndroidRuntime(873):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-19 13:17:32.934: E/AndroidRuntime(873):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-19 13:17:32.934: E/AndroidRuntime(873):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 13:17:32.934: E/AndroidRuntime(873):  at android.os.Looper.loop(Looper.java:137)
06-19 13:17:32.934: E/AndroidRuntime(873):  at android.app.ActivityThread.main(ActivityThread.java:4424)
06-19 13:17:32.934: E/AndroidRuntime(873):  at java.lang.reflect.Method.invokeNative(Native Method)
06-19 13:17:32.934: E/AndroidRuntime(873):  at java.lang.reflect.Method.invoke(Method.java:511)
06-19 13:17:32.934: E/AndroidRuntime(873):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-19 13:17:32.934: E/AndroidRuntime(873):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-19 13:17:32.934: E/AndroidRuntime(873):  at dalvik.system.NativeStart.main(Native Method)
06-19 13:17:32.934: E/AndroidRuntime(873): Caused by: java.lang.NullPointerException
06-19 13:17:32.934: E/AndroidRuntime(873):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
06-19 13:17:32.934: E/AndroidRuntime(873):  at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:231)
06-19 13:17:32.934: E/AndroidRuntime(873):  at achira.test.DatabaseHelper.getAllDepts(DatabaseHelper.java:126)
06-19 13:17:32.934: E/AndroidRuntime(873):  at achira.test.Utilities.ManageDeptSpinner(Utilities.java:13)
06-19 13:17:32.934: E/AndroidRuntime(873):  at achira.test.GridList.onCreate(GridList.java:35)
06-19 13:17:32.934: E/AndroidRuntime(873):  at android.app.Activity.performCreate(Activity.java:4465)
06-19 13:17:32.934: E/AndroidRuntime(873):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-19 13:17:32.934: E/AndroidRuntime(873):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-19 13:17:32.934: E/AndroidRuntime(873):  ... 11 more
06-19 13:17:33.114: D/dalvikvm(873): GC_CONCURRENT freed 62K, 2% free 11769K/11975K, paused 5ms+60ms
4

1 回答 1

1

这是给您 NullPointerException 的行:

db = mContext.openOrCreateDatabase(mName, 0, mFactory, mErrorHandler);

所以问题是 mContext 是 NULL。执行 super(context, dbName, null,40); 时提供上下文

确保为辅助构造函数提供有效的上下文:DatabaseHelper(Context context), NULL is not valid context ;)

于 2012-06-19T08:14:03.760 回答