0
public class DatabaseHandler extends SQLiteOpenHelper {


// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "contextsManager";

// Locations table name
private static final String TABLE_LOCATIONLABLES = "locationLables";

// LOCATIONLABLES Table Columns names
private static final String KEY_LOCID = "loc_id";
private static final String KEY_LOCNAME = "loc_name";

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String TABLE_LOCATIONLABLES = "CREATE TABLE " + TABLE_LOCATIONLABLES + "("
            + KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT,"
            +  ")";
    db.execSQL(TABLE_LOCATIONLABLES);   
}

这是说局部变量 TABLE_LOCATIONLABLES 可能尚未初始化?它被初始化了这个错误是怎么发生的?

4

7 回答 7

4

问题在于你的代码本身

 // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String TABLE_LOCATIONLABLES = "CREATE TABLE " + TABLE_LOCATIONLABLES + "("
                + KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT,"
                +  ")";
        db.execSQL(TABLE_LOCATIONLABLES);   
    }

在 oncreate(db) 中,您TABLE_LOCATIONLABLES再次声明并在同一行中使用它,这就是您得到的原因Local variable may not have been initialized

只需像这样在 oncreate(db) 中重命名 String TABLE_LOCATIONLABLES

    @Override
    public void onCreate(SQLiteDatabase db) {
        String mQuery = "CREATE TABLE " + TABLE_LOCATIONLABLES + "("
                + KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT,"
                +  ")";
        db.execSQL(mQuery);   
    }
于 2012-08-08T08:17:37.473 回答
3

您正在访问TABLE_LOCATIONLABLES以创建字符串。局部变量与静态变量具有相同的名称,因此选择了局部变量。您应该重命名局部变量来解决这个问题。

于 2012-08-08T08:14:51.047 回答
2

您要声明 TABLE_LOCATIONLABLES 两次。一次在类级别,一次在 onCreate 例程中。只需删除 onCreate 例程中的声明即可。

报废这个。我刚刚看到下面的答案。完全忽略了您的类声明是静态字段的事实。

于 2012-08-08T08:13:37.783 回答
1

你有:

String TABLE_LOCATIONLABLES = "CREATE TABLE " + TABLE_LOCATIONLABLES + "("
     /*^^^^^^^^^^^^^^^^^^^^*/                 /*^^^^^^^^^^^^^^^^^^^^*/
        + KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT,"
        +  ")";

请注意,您TABLE_LOCATIONLABLES在那里提到了两次!虽然重用静态成员名称可能是一种不好的风格,但快速解决方法是:

String TABLE_LOCATIONLABLES = "CREATE TABLE " + DatabaseHandler.TABLE_LOCATIONLABLES + "("
                                              /*^^^^^^^^^^^^^^^^*/
        + KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT,"
        +  ")";

(PS:是拼写LABELS,不是LABLES!)

于 2012-08-08T08:19:34.543 回答
0
public void onCreate(SQLiteDatabase db) { 
TABLE_LOCATIONLABLES = "CREATE TABLE " + TABLE_LOCATIONLABLES + "(" 
        + KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT," 
        +  ")"; 
db.execSQL(TABLE_LOCATIONLABLES);    
} 
于 2012-08-08T08:19:17.753 回答
0

OnCreate()方法中,您尝试访问作为实例变量的变量,但它实际上仅访问本地方法变量。我建议您按如下方式更新您的代码,这样它肯定会起作用,

public void onCreate(SQLiteDatabase db) {
    String TBL_LOCATIONLABLES = "CREATE TABLE " + TABLE_LOCATIONLABLES + "("
            + KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT,"
            +  ")";
    db.execSQL(TBL_LOCATIONLABLES);   
}
于 2012-08-08T08:20:07.690 回答
0

您有一个名为TABLE_LOCATIONLABLES的静态变量。在您的onCreate方法中,您正在初始化一个具有相同名称的变量 - 直到现在都没有问题。但是在您的声明中 ,您正在访问局部变量 TABLE_LOCATIONLABLES!尽管您在语句中分配局部变量时正在初始化局部变量,但此时局部变量尚未初始化。String TABLE_LOCATIONLABLES = "CREATE TABLE " + TABLE_LOCATIONLABLES + "(" + KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT," + ")";¸

我认为您尝试访问静态变量,因此您必须像DatabaseHandler.TABLE_LOACATIONLABLES一样访问它。

用这个替换你的旧语句: String TABLE_LOCATIONLABLES = "CREATE TABLE " + DatabaseHandler.TABLE_LOCATIONLABLES + "(" + DatabaseHandler.KEY_LOCID + " INTEGER PRIMARY KEY," + DatabaseHandler.KEY_LOCNAME + " TEXT," + ")";¸

一个小提示:如果变量是成员或静态变量,则通过此访问变量,因此更容易查看变量的实例化位置。

问候

于 2012-08-08T08:27:36.903 回答