我已經被Stucked以獲取資產文件夾中的數據(包含插入SQL)到數據庫中。我試過了很大的變化,但我比較報告:ANDROID - 將數據插入數據庫,報告無法啓動活動ComponentInfo:java.lang.NullPointerException
"Unable to start activity ComponentInfo{com.restaurant.sesame/com.restaurant.sesame.Food}: java.lang.NullPointerException"
這是我food.txt文件內容:
INSERT INTO Food (name,detail,price)VALUES ('Cow Rib Steak', '1h 37m Shipping: $11.00', '11.00')
INSERT INTO Food (name,detail,price)VALUES ('Cow Rib Steak', '1h 37m Shipping: $11.00', '11.00')
INSERT INTO Food (name,detail,price)VALUES ('Cow Rib Steak', '1h 37m Shipping: $11.00', '11.00')
INSERT INTO Food (name,detail,price)VALUES ('Cow Rib Steak', '1h 37m Shipping: $11.00', '11.00')
我一直在掙扎了很長一段時間。我訪問了很多網站來研究這一點。 有人可以提出一個更堅實的方法來幫助我解決這個錯誤嗎?
public class Restaurant extends Activity {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_DETAIL = "detail";
public static final String KEY_PRICE = "price";
private static final String DATABASE_NAME ="Restaurantdb";
private static final String DATABASE_TABLE ="Food";
private static final int DATABASE_VERSION = 1;
private SQLiteDatabase ourDatabase;
private final Context ourContext;
private DBHelper ourHelper;
InputStream input;
AssetManager assetManager = getAssets();
static String value;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
try {
input = assetManager.open("food.txt");
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
// byte buffer into a string
value = new String(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE Food (" + KEY_ROWID +" INTEGER PRIMARY KEY AUTOINCREMENT, "
+KEY_NAME+" TEXT NOT NULL, "+KEY_DETAIL+ " TEXT NOT NULL, " + KEY_PRICE+ " DECIMAL (18,2) NOT NULL);"
);
db.execSQL("CREATE TABLE Bill (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL,"+
" price DECIMAL (18,2) NOT NULL, quantity INTEGER NOT NULL, total DECIMAL (18,2) NOT NULL);"
);
db.rawQuery(value, null);
Log.w("my app", "insert data into NEW DB");
}
給一些stacktrace t o找到問題出在哪裏 – Chet 2012-04-01 13:13:42
stacktrace?對不起,我不明白。 – 2012-04-01 14:08:47
我的意思是你所謂的「補償報告」,即「無法啓動..」。請發佈剩餘的線路,以便跟蹤問題的出處! – Chet 2012-04-01 14:21:06