局部变量可能尚未初始化
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可能没有被初始化?它初始化了这个错误是怎么发生的?局部变量可能尚未初始化
万阿英,蒋达清在于withing你的代码本身
// 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
只是重命名字符串TABLE_LOCATIONLABLES里面你的OnCreate(分贝)这样
@Override
public void onCreate(SQLiteDatabase db) {
String mQuery = "CREATE TABLE " + TABLE_LOCATIONLABLES + "("
+ KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT,"
+ ")";
db.execSQL(mQuery);
}
您正在声明TABLE_LOCATIONLABLES两次。一旦进入课程级别,一旦进入onCreate例程。只需删除onCreate例程中的声明即可。
碎片这。我刚刚看到下面的答案。完全错过了你的类声明是一个静态字段的事实。
您正在访问TABLE_LOCATIONLABLES
以创建字符串。局部变量与静态变量名称相同,因此本地变量被选中。你应该重命名局部变量来解决这个问题。
public void onCreate(SQLiteDatabase db) {
TABLE_LOCATIONLABLES = "CREATE TABLE " + TABLE_LOCATIONLABLES + "("
+ KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT,"
+ ")";
db.execSQL(TABLE_LOCATIONLABLES);
}
您有:
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
!)
感谢不同的方式:) – user340 2012-08-08 09:45:54
在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);
}
你有一个静态变量称为TABLE_LOCATIONLABLES。在您的onCreate方法中,您正在使用一个名称相同的变量 - 直到现在没有问题。但在您的声明String TABLE_LOCATIONLABLES = "CREATE TABLE " + TABLE_LOCATIONLABLES + "(" + KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT," + ")";¸
您正在访问局部变量TABLE_LOCATIONLABLES!虽然您正在创新您的本地变量,同时在您的对帐单中进行分配,但此时本地变量未初始化。
我想你想访问你的静态变量因此,你必须访问它像DatabaseHandler.TABLE_LOACATIONLABLES。
与此替换旧的语句: String TABLE_LOCATIONLABLES = "CREATE TABLE " + DatabaseHandler.TABLE_LOCATIONLABLES + "(" + DatabaseHandler.KEY_LOCID + " INTEGER PRIMARY KEY," + DatabaseHandler.KEY_LOCNAME + " TEXT," + ")";¸
小提示:访问您的变量,如果他们通过这个成员或静态的,所以它更容易看到其中的变量被实例化。
Greets
尝试清理您的项目? – 2012-08-08 08:14:15