当我在logcat中将数据插入sqlite数据库时,它会提示空对象引用。但是使用我在另一个程序中使用的相同代码它可以工作。我能否提出一些解决这个问题的建议。
这是我的DBControl
public void addProduct(Product product) {
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(Code,product.getProductCode());
cv.put(Description,product.getProductName());
cv.put(MenuDepartment,product.getMenuDepartment());
cv.put(CondimentGroup,product.getCondimentGroup1());
cv.put(ModifierGroup,product.getModifierGroup1());
//inserting row
db.insert(mproduct, null, cv);
//close the database to avoid any leak
db.close();
}
这是按钮点击时插入数据的方式。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_printer_test_demo);
innitView();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_printer_test_demo);
innitView();
}
private void innitView() {
new BitmapUtils(this);
b_table = (Button) findViewById(R.id.b_table);
b_additem = (Button)findViewById(R.id.additem);
findViewById(R.id.additem).setOnClickListener(this);
findViewById(R.id.b_exit).setOnClickListener(this);
b_additem.setOnClickListener(this);
b_table.setOnClickListener(this);
info = (TextView) findViewById(R.id.info);
callback = new ICallback.Stub() {
@Override
public void onRunResult(final boolean success) throws RemoteException {
}
@Override
public void onReturnString(final String value) throws RemoteException {
Log.i(TAG,"printlength:" + value + "\n");
}
@Override
public void onRaiseException(int code, final String msg) throws RemoteException {
Log.i(TAG,"onRaiseException: " + msg);
runOnUiThread(new Runnable(){
@Override
public void run() {
info.append("onRaiseException = " + msg + "\n");
}});
}
};
setButtonEnable(false);
Intent intent=new Intent();
intent.setPackage("woyou.aidlservice.jiuiv5");
intent.setAction("woyou.aidlservice.jiuiv5.IWoyouService");
startService(intent);
bindService(intent, connService, Context.BIND_AUTO_CREATE);
}
private void setButtonEnable(boolean flag){
b_table.setEnabled(flag);
b_additem.setEnabled(flag);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.b_table:
//打印表格
printTable();
break;
case R.id.additem:
additem();
break;
}
}
public void additem(){
try {
DBControl db = new DBControl(context);<------the logcat show the error at here
db.addProduct(new Product("10001", "Nasi Lemak", "A", "C1", "M1"));
db.addProduct(new Product("10002", "STEAM LOTUS LEAF GLUTINOUS RICE", "A", "C1", "M1"));
db.addProduct(new Product("10003", "EIGHT TREASURE GLUTINOUS RICE", "B", "C2", "M2"));
db.addProduct(new Product("10004", "CHICKEN PORRIDGE & DRIED SCALLOP", "B", "C2", "M2"));
db.addProduct(new Product("10005", "FISH PORRIDGE", "C", "C3", "M3"));
db.addProduct(new Product("10006", "LAKSA", "C", "C3", "M3"));
}catch(SQLiteException e){
e.printStackTrace();
}
}
logcat的
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.test.printertestdemo.DBControl.addProduct(com.test.printertestdemo.Constructor.Product)' on a null object reference
at com.test.printertestdemo.PrinterTestDemoAct.additem(PrinterTestDemoAct.java:181)
at com.test.printertestdemo.PrinterTestDemoAct.onClick(PrinterTestDemoAct.java:173)
at android.view.View.performClick(View.java:4848)
at android.view.View$PerformClick.run(View.java:20262)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
答案 0 :(得分:0)
on your create define
Context context = getContext();
调用方法项传递上下文
addItem(context);
在你的方法中添加参数Context context; 那就行了。
如果不起作用,请使用
Activity context = getActivity();
第一步。 希望它有所帮助。