Ok I'm stuck at this point where I'm trying to insert data into sqlite database. It's crashing cause it is saying my db is null I think. I think I am not initializing (or maybe another term... basically to recognize what I did in the other class) in my Additem class but not sure how to solve and why exactly.
好,现在我要把数据插入到sqlite数据库中。它崩溃了,因为它说我的db是空的。我想我不是在初始化(或者可能是另一个术语……)基本上是为了识别我在其他课上做了什么)在我的加法课上但不知道如何解决和为什么。
Pointers?
指针?
Additem.class
Additem.class
public class AddItem extends Activity implements OnClickListener {
private final String TAG = "Main Activity";
View v;
SQLiteDatabase db;
DbHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_item);
Log.i(TAG, "OnCreate");
}
@Override
public void onClick(View v) {
Log.i(TAG, "Extracting Data");
DatePicker datePicker1 = (DatePicker) findViewById(R.id.datePicker1);
AutoCompleteTextView autoCompleteSubCat = (AutoCompleteTextView) findViewById(R.id.autoCompleteSubCat);
EditText editItem = (EditText) findViewById(R.id.editItem);
EditText editPrice = (EditText) findViewById(R.id.editPrice);
EditText editQuantity = (EditText) findViewById(R.id.editQuantity);
EditText editWeight = (EditText) findViewById(R.id.editWeight);
EditText editVolume = (EditText) findViewById(R.id.editVolume);
//CheckBox checkSale = (CheckBox) findViewById(R.id.checkSale);
AutoCompleteTextView autoCompleteStore = (AutoCompleteTextView) findViewById(R.id.autoCompleteStore);
EditText editExtra = (EditText) findViewById(R.id.editExtra);
String subcat,item,store,extra;
Integer day,month,year;
Double price,quantity,weight,volume,sale;
Log.i(TAG, "Converting to String and Int");
day = datePicker1.getDayOfMonth();
month = datePicker1.getMonth();
year = datePicker1.getYear();
subcat = autoCompleteSubCat.getText().toString();
item = editItem.getText().toString();
extra = editExtra.getText().toString();
price = Double.parseDouble(editPrice.getText().toString());
quantity = Double.parseDouble(editQuantity.getText().toString());
weight = Double.parseDouble(editWeight.getText().toString());
volume = Double.parseDouble(editVolume.getText().toString());
// sale = checkSale;
store = autoCompleteStore.getText().toString();
db = dbHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(DbPrice.SUBCAT, subcat);
cv.put(DbPrice.ITEM, item);
cv.put(DbPrice.PRICE, price);
cv.put(DbPrice.QUANTITY, quantity);
cv.put(DbPrice.WEIGHT, weight);
cv.put(DbPrice.VOLUME, volume);
// cv.put(DbPrice.SALE, sale);
cv.put(DbPrice.STORE, store);
cv.put(DbPrice.EXTRA, extra);
db.insert(DbPrice.TABLE_NAME, null, cv);
dbHelper.close();
Log.i(TAG, "Starting New Activity");
Intent allItemsActivity = new Intent (AddItem.this, AllItems.class);
startActivity(allItemsActivity);
}
DbPrice.class
DbPrice.class
public class DbPrice extends Activity {
public static final String DATABASE_NAME = "data";
public static final String TABLE_NAME = "price_table";
public static final String C_ID = "_id";
public static final String DAY = "day";
public static final String MONTH = "month";
public static final String YEAR = "year";
public static final String SUBCAT = "subcategory";
public static final String ITEM = "item";
public static final String PRICE = "price";
public static final String QUANTITY = "quantity";
public static final String WEIGHT = "weight";
public static final String VOLUME = "volume";
public static final String SALE = "sale";
public static final String STORE = "store";
public static final String EXTRA = "extra";
public static final int VERSION = 1;
Context context;
DbHelper dbHelper;
SQLiteDatabase db;
public DbPrice(Context context) {
this.context = context;
dbHelper = new DbHelper(context);
}
public Cursor query() {
db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(DbPrice.TABLE_NAME,null, null, null, null, null, SUBCAT + " DESC");
return cursor;
}
class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
private final String createDb = "create table if not exists " + TABLE_NAME+ " ( "
+ C_ID + " integer primary key autoincrement, "
// + DAY + " integer, "
// + MONTH + " integer, "
// + YEAR + " integer, "
+ SUBCAT + " text, "
+ ITEM + " text, "
+ PRICE + " integer, "
+ QUANTITY + " integer, "
+ WEIGHT + " integer, "
+ VOLUME + " integer, "
// + SALE + " text, "
+ STORE + " text, "
+ EXTRA + " text) ";
@Override
public void onCreate(SQLiteDatabase db) {
Log.d("DbPrice", "Oncreate with SQL"+ createDb);
db.execSQL(createDb);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
db.execSQL("drop table if exists " + TABLE_NAME);
onCreate(db);
}
}
}
In my GroceryApp.class (Activity class, I did this)
在我GroceryApp。类(活动类,我做了这个)
public class GroceryApp extends Application {
static final String TAG = "GroceryApp";
DbPrice dbPrice;
@Override
public void onCreate() {
super.onCreate();
dbPrice = new DbPrice(this);
Log.d(TAG, "OnCreated GroceryApp");
}
}
Here's my logcat, just fyi
这是我的logcat
01-10 06:18:38.763: E/AndroidRuntime(1119): java.lang.IllegalStateException: Could not execute method of the activity
01-10 06:18:38.763: E/AndroidRuntime(1119): at android.view.View$1.onClick(View.java:3814)
01-10 06:18:38.763: E/AndroidRuntime(1119): at android.view.View.performClick(View.java:4424)
01-10 06:18:38.763: E/AndroidRuntime(1119): at android.view.View$PerformClick.run(View.java:18383)
01-10 06:18:38.763: E/AndroidRuntime(1119): at android.os.Handler.handleCallback(Handler.java:733)
01-10 06:18:38.763: E/AndroidRuntime(1119): at android.os.Handler.dispatchMessage(Handler.java:95)
01-10 06:18:38.763: E/AndroidRuntime(1119): at android.os.Looper.loop(Looper.java:137)
01-10 06:18:38.763: E/AndroidRuntime(1119): at android.app.ActivityThread.main(ActivityThread.java:4998)
01-10 06:18:38.763: E/AndroidRuntime(1119): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 06:18:38.763: E/AndroidRuntime(1119): at java.lang.reflect.Method.invoke(Method.java:515)
01-10 06:18:38.763: E/AndroidRuntime(1119): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-10 06:18:38.763: E/AndroidRuntime(1119): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-10 06:18:38.763: E/AndroidRuntime(1119): at dalvik.system.NativeStart.main(Native Method)
01-10 06:18:38.763: E/AndroidRuntime(1119): Caused by: java.lang.reflect.InvocationTargetException
01-10 06:18:38.763: E/AndroidRuntime(1119): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 06:18:38.763: E/AndroidRuntime(1119): at java.lang.reflect.Method.invoke(Method.java:515)
01-10 06:18:38.763: E/AndroidRuntime(1119): at android.view.View$1.onClick(View.java:3809)
01-10 06:18:38.763: E/AndroidRuntime(1119): ... 11 more
01-10 06:18:38.763: E/AndroidRuntime(1119): Caused by: java.lang.NullPointerException
01-10 06:18:38.763: E/AndroidRuntime(1119): at com.unsuccessfulstudent.grocerypricehistory.AddItem.onClick(AddItem.java:73)
2 个解决方案
#1
1
first you have to create the object of
DbHelper in oncreate like this DbHelper dbHelper = new DbHelper(AddIten.this);
首先,必须在oncreate中创建DbHelper对象,如DbHelper DbHelper = new DbHelper(additenthis);
#2
1
You forgot to initialize DbHelper dbHelper;
忘记初始化DbHelper DbHelper;
dbHelper = new DbHelper(AddIten.this);
in onCreate
在onCreate
But you have
但你有
public class DbPrice extends Activity {
I think you need to look at the docs. There are more mistakes.
我想你需要看看医生。有更多的错误。
And you are creating an instance of activity class which is very wrong
你正在创建一个活动类的实例,这是非常错误的。
dbPrice = new DbPrice(this);
Edit:
编辑:
Have the below in DbPrice
DbPrice是否包含以下内容
public DbPrice(Context context) {
this.context = context;
}
public DbPrice open()throws SQLException
{
dbHelper= new DbHelper(context);
db= dbHelper.getWritableDatabase();
return this;
}
public void addContent(String subcat,String item,Double price2,Double quantity2,Double weight2,Double volume2,String store,String extra)
{
ContentValues cv = new ContentValues();
cv.put("SUBCAT", subcat);
cv.put(DbPrice.ITEM, item);
cv.put(DbPrice.PRICE, price2);
cv.put(DbPrice.QUANTITY, quantity2);
cv.put(DbPrice.WEIGHT, weight2);
cv.put(DbPrice.VOLUME, volume2);
// cv.put(DbPrice.SALE, sale);
cv.put(DbPrice.STORE, store);
cv.put(DbPrice.EXTRA, extra);
db.insert(DbPrice.TABLE_NAME, null, cv);
}
And in Activity
在活动
dbHelper = new DbPrice(MainActivity.this);
DbPrice db= dbHelper.open();
db.addContent(subcat,item,price,quantity,weight,volume,store,extra);
#1
1
first you have to create the object of
DbHelper in oncreate like this DbHelper dbHelper = new DbHelper(AddIten.this);
首先,必须在oncreate中创建DbHelper对象,如DbHelper DbHelper = new DbHelper(additenthis);
#2
1
You forgot to initialize DbHelper dbHelper;
忘记初始化DbHelper DbHelper;
dbHelper = new DbHelper(AddIten.this);
in onCreate
在onCreate
But you have
但你有
public class DbPrice extends Activity {
I think you need to look at the docs. There are more mistakes.
我想你需要看看医生。有更多的错误。
And you are creating an instance of activity class which is very wrong
你正在创建一个活动类的实例,这是非常错误的。
dbPrice = new DbPrice(this);
Edit:
编辑:
Have the below in DbPrice
DbPrice是否包含以下内容
public DbPrice(Context context) {
this.context = context;
}
public DbPrice open()throws SQLException
{
dbHelper= new DbHelper(context);
db= dbHelper.getWritableDatabase();
return this;
}
public void addContent(String subcat,String item,Double price2,Double quantity2,Double weight2,Double volume2,String store,String extra)
{
ContentValues cv = new ContentValues();
cv.put("SUBCAT", subcat);
cv.put(DbPrice.ITEM, item);
cv.put(DbPrice.PRICE, price2);
cv.put(DbPrice.QUANTITY, quantity2);
cv.put(DbPrice.WEIGHT, weight2);
cv.put(DbPrice.VOLUME, volume2);
// cv.put(DbPrice.SALE, sale);
cv.put(DbPrice.STORE, store);
cv.put(DbPrice.EXTRA, extra);
db.insert(DbPrice.TABLE_NAME, null, cv);
}
And in Activity
在活动
dbHelper = new DbPrice(MainActivity.this);
DbPrice db= dbHelper.open();
db.addContent(subcat,item,price,quantity,weight,volume,store,extra);