2013-09-26 34 views
0

你好我正在爲我的android應用程序構建一個SQLite Db。這是代碼: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;我在這裏失去了一些東西初始化?提前感謝!

回答

1

你沒有初始化上下文呢。你必須初始化它FeedsDbAdapter構造函數中,如:

public FeedsDbAdapter (Context context){ 

     mCtx = context; 
    } 

首先聲明FeedsDbAdapter構造,因爲你通過它的構造在其他班級使用FeedsDbAdapter類ANS的其他活動方面將分配給此當前上下文。

+0

感謝很多!它工作完美 – SoCo

+0

很高興幫助你..所有最好的。 – Ranjit

1

你的代碼更改爲:

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

     mCtx = context; 
    } 
+0

我做了你告訴我要做的事情,但錯誤不是固定的。它在'mCtx = context;'上顯示錯誤:'不能對非靜態字段mCtx做出靜態引用「 – SoCo

0

你的這個問題涉及到Java。該行private final Context mCtx;只是類型上下文的字段mCtx的聲明。由於該字段已被宣佈爲最終版本,因此必須在構造函數mCtx = context;內部隱藏。即使mCtx未被聲明爲final,在使用之前仍需要初始化。

0

那是因爲你的類是靜態的......從您的類的靜態和你的代碼更改

DatabaseHelper(Context context) { 
    super(context, DB_NAME, null, DB_VERSION); 
    mCtx = context; 
}