0

我在访问 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);   
4

2 回答 2

2

这是因为Fragments 需要附加到一个 Activity 上(查看Fragments 上的文档才能返回一个 Activity。特别是生命周期。首先将 Fragment 附加到一个 Activity 上(并被onAttach()调用。然后 Fragment 可以获取一个 Activity 实例。

DataBasex con=new DataBasex(getSherlockActivity());

位于 Class 的根目录中,它将返回 null - Fragment 立即没有 Activity。

改变

DataBasex con=new DataBasex(getSherlockActivity());

所以它是:

DataBasex con;

并添加此方法:

@Override
public void onCreate (Bundle b)
{
  super.onCreate (b);
  con = new DataBasex(getSherlockActivity());
}
于 2013-01-17T02:36:43.857 回答
0

原因是在静态类中没有对象,方法的访问将是类级别的访问,但在非静态类中会有该类的对象,那么你应该没有空值在非静态类中

于 2013-01-17T02:36:31.890 回答