Reputation: 973
i've problem access other class in android... i've class databasex (non static) and class CountingFragment (static).... in CountingFragment i want to access databasex class..but i get result null.... here is my piece of code...
class Databasex
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();
}
}
and here is CountingFragment class...
public static class CountingFragment extends SherlockFragment {
//here myproblem...
DataBasex con=new DataBasex(getSherlockActivity());
/**
*other method
*/
}
i think the code bellow is my problem
DataBase con=new DataBase(getSherlockActivity());
if in non-static class i use code bellow and everything is ok....but in static class i can't...why??
DataBase con=new DataBase(this);
Upvotes: 0
Views: 98
Reputation: 36449
It is due to the fact that Fragment
s need to first be attached to an Activity (see the document on Fragment
s before they can return an Activity. Specifically the lifecycle. First the Fragment is attached to an Activity (and onAttach()
is called. Then the Fragment can get an Activity instance.
DataBasex con=new DataBasex(getSherlockActivity());
is in the root of the Class, it will return null - the Fragment does not have an Activity right off the bat.
Change
DataBasex con=new DataBasex(getSherlockActivity());
So it is:
DataBasex con;
And add this method:
@Override
public void onCreate (Bundle b)
{
super.onCreate (b);
con = new DataBasex(getSherlockActivity());
}
Upvotes: 2
Reputation: 473
The reason for that is because in Static class there will be No Object, and the access of the methods will be class level access, but in the none-static class there will be an Object of that class, then you should have no null value in the none-static class
Upvotes: 0