我在访问 android 中的其他类时遇到问题...我有类 databasex(非静态)和类 CountingFragment(静态)....在 CountingFragment 我想访问 databasex 类..但我得到的结果为空....这是我的一段代码...
类数据库x
public class DataBasex {
public static final String KEY_ROWID="_id";
public static final String KEY_NAME="namaLokasi";
public static final String KEY_JENIS="jenis";
public static final String KEY_KETERANGAN="ket";
public static final String tanda="DataBasex";
private static final String DATABASE_NAME="DbPeta";
private static final String DATABASE_TABLE="lokasi";
private static final int DATABASE_VERSION=1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.i(tanda,"dbHelper-> "+context);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Log.i(tanda,"onCreate-> "+db);
/*db.execSQL("CREATE TABLE " + DATABASE_TABLE + "("+
KEY_ROWID +" INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME +" TEXT NOT NULL, " + KEY_JENIS +" TEXT NOT NULL, " +
KEY_KETERANGAN +" TEXT NOT NULL);");
*/
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.i(tanda,"onUpgrade-> "+db);
//db.execSQL("DROP TABLE IF EXISTS"+DATABASE_TABLE);
//onCreate(db);
}
}
public DataBasex(Context c){
ourContext =c;
Log.i(tanda,"DataBasex-> "+ourContext);
}
public DataBasex open() throws SQLException{
Log.i(tanda,"DataBasex open awal ");
ourHelper = new DbHelper(ourContext);
ourDatabase=ourHelper.getWritableDatabase();
Log.i(tanda,"DataBasex open-> "+ourDatabase);
return this;
}
public void close(){
Log.i(tanda,"[close]");
ourHelper.close();
}
}
这是 CountingFragment 类...
public static class CountingFragment extends SherlockFragment {
//here myproblem...
DataBasex con=new DataBasex(getSherlockActivity());
/**
*other method
*/
}
我认为下面的代码是我的问题
DataBase con=new DataBase(getSherlockActivity());
如果在非静态类中我使用下面的代码并且一切正常……但在静态类中我不能……为什么?
DataBase con=new DataBase(this);