0

你好,我正在为我的 android 应用程序构建一个 SQLite Db。这是代码:

package com.example.pap_e;



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

public class FeedsDbAdapter {

    private final Context mCtx;
    private static final String DEBUG_TAG = "RSSDatabase";
    private static final int DB_VERSION = 1;
    private static final String DB_NAME = "rss_data";
    public static String TABLE = "list";
    public static final String ID = "_id";
    public static final String RSS = "_rss";
    public static final String TITLE = "_title";
    public static final String PUBDATE = "_pubdate";
    public static final String DESCRIPTION = "_description";
    public static final String LINK = "_link";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;



    private static class DatabaseHelper extends SQLiteOpenHelper{
        DatabaseHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
        }


        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE_TABLE"+TABLE+"("+ID+ "integer PRIMARY KEY AUTOINCREMENT,"+RSS+"text NOT NULL,"
                    +TITLE+"text NOT NULL,"+PUBDATE+"text NOT NULL,"+DESCRIPTION+"text NOT NULL,"+LINK+"text NOT NULL"+")");
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE);
            onCreate(db);}

    }
    public FeedsDbAdapter open() throws SQLException {

        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
        }
        public void close() {
        mDbHelper.close();
        }

        public boolean updateAnakoinosi(long rowId, String rss, String title,
                String pubdate, String description, String link) {
                ContentValues args = new ContentValues();
                args.put(RSS, rss);
                args.put(TITLE, title);
                args.put(PUBDATE, pubdate);
                args.put(DESCRIPTION, description);
                args.put(LINK, link);
                return mDb.update(TABLE, args, ID + "=" + rowId, null) > 0;}
                public Cursor fetchAllAnakoinoseis() {
                return mDb.query(TABLE, new String[] { ID, RSS, TITLE, PUBDATE,
                DESCRIPTION,LINK }, null, null, null, null, null); }



}

问题是我收到一个错误public class FeedsDbAdapter,上面写着:“空白的最终字段 mCtx 可能尚未初始化”但我使用它初始化private final Context mCtx;了我在这里遗漏了什么吗?提前非常感谢!

4

4 回答 4

1

将您的代码更改为:

private static class DatabaseHelper extends SQLiteOpenHelper{
    DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);

        mCtx = context;
    }
于 2013-09-26T18:04:27.063 回答
1

您尚未初始化上下文。您必须在 FeedsDbAdapter 构造函数中对其进行初始化,例如:

public FeedsDbAdapter (Context context){

       mCtx = context;
     }

首先声明 FeedsDbAdapter 构造函数,因为您在其他类中通过其构造函数使用 FeedsDbAdapter 类,因为其他活动上下文将分配给当前上下文。

于 2013-09-26T18:54:57.057 回答
0

那是因为你的类是静态的......从你的类中删除静态并将你的代码更改为

DatabaseHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    mCtx = context;
}
于 2013-09-26T18:37:40.813 回答
0

你的这个问题与java有关。这一行private final Context mCtx;只是一个 context 类型的字段 mCtx 的声明。并且由于该字段已被声明为 final,因此必须在构造函数中进行初始化mCtx = context;。即使 mCtx 没有被声明为 final,它仍然需要在使用前进行初始化。

于 2013-09-26T18:09:03.057 回答