-1
我按照在線教程,創建了一個使用ZXing庫掃描條形碼和QR碼的應用程序,並將所有信息存儲在數據庫中。我跟着一步一步做的事,但在我的代碼.The數據不能在數據庫存儲,並保持部隊密切Zxing應用程序和SQLite數據庫
Calledspot_pay.java
package com.example.zxing_android_products;
import java.math.BigDecimal;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.support.v4.app.NavUtils;
public class Calledspot_pay extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calledspot_pay);
Button addButton=(Button)findViewById(R.id.addMenuButton);
addButton.setOnClickListener (new OnClickListener(){
public void onClick (View v){
startActivity(new Intent(Calledspot_pay .this, AddProduct.class));
}
});
}
static final class ProductData {
String barcode;
String format;
String title;
BigDecimal price;
}
}
ProductDatabase.java
package com.example.zxing_android_products;
import java.math.BigDecimal;
import com.example.zxing_android_products.Calledspot_pay.ProductData;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class ProductDatabase {
private static final String PRODUCT_TABLE="products";
private static final String DATABASE_NAME="spot_pay.db";
private static final int DATABASE_VERSION=1;
private static final BigDecimal ONE_HUNDRED = null;
private SQLiteDatabase db;
private static class ProductDatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = null;
public ProductDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
StringBuilder sql = new StringBuilder();
sql.append("create table ").append(PRODUCT_TABLE)
.append("( ")
.append(" _id integer primary key,")
.append(" barcode text,")
.append(" format text,")
.append(" title text,")
.append(" price currency")
.append(") ");
db.execSQL(sql.toString());
Log.d(TAG, PRODUCT_TABLE + "table created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + PRODUCT_TABLE);
onCreate(db);
}
}
public ProductDatabase(Context context) {
ProductDatabaseHelper helper = new ProductDatabaseHelper(context);
db = helper.getWritableDatabase();
}
public boolean insert(ProductData product) {
ContentValues vals = new ContentValues();
vals.put("barcode", product.barcode);
vals.put("format", product.format);
vals.put("title", product.title);
vals.put("price", product.price.multiply(ONE_HUNDRED).longValue());
return db.insert(PRODUCT_TABLE, null, vals) != -1;
}
}
AddProduct.java
package com.example.zxing_android_products;
import java.math.BigDecimal;
import com.example.zxing_android_products.Calledspot_pay.ProductData;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AddProduct extends Activity implements OnClickListener{
private static final int REQUEST_BARCODE=0;
private static final ProductData mProductData=new ProductData();
EditText mBarcodeEdit;
EditText mFormatEdit;
EditText mTitleEdit;
EditText mPriceEdit;
private Button mScanButton;
private Button mAddButton;
//private ProductDatabase mProductDb;
ProductDatabase mProductDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_product);
mBarcodeEdit = (EditText) findViewById(R.id.barcodeEdit);
mFormatEdit = (EditText) findViewById(R.id.codeFormatEdit);
mTitleEdit = (EditText) findViewById(R.id.titleEdit);
mPriceEdit = (EditText) findViewById(R.id.priceEdit);
mScanButton = (Button) findViewById(R.id.scanButton);
mScanButton.setOnClickListener(this);
mAddButton = (Button) findViewById(R.id.addButton);
mAddButton.setOnClickListener(this);
mProductDb = new ProductDatabase(this); // not yet shown
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.scanButton:
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, REQUEST_BARCODE);
break;
case R.id.addButton:
String barcode = mBarcodeEdit.getText().toString();
String format = mFormatEdit.getText().toString();
String title = mTitleEdit.getText().toString();
String price = mPriceEdit.getText().toString();
String errors = validateFields(barcode, format, title, price);
if (errors.length() > 0) {
showInfoDialog(this, "Please fix errors", errors);
} else {
mProductData.barcode = barcode;
mProductData.format = format;
mProductData.title = title;
mProductData.price = new BigDecimal(price);
mProductDb.insert(mProductData);
showInfoDialog(this, "Success", "Product saved successfully");
resetForm();
}
break;
}
}
private void showInfoDialog(Context context, String title, String information) {
new AlertDialog.Builder (context)
.setMessage(information)
.setTitle(title)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
private void resetForm() {
// TODO Auto-generated method stub
mBarcodeEdit.getText().clear();
mFormatEdit.getText().clear();
mTitleEdit.getText().clear();
mPriceEdit.getText().clear();
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_BARCODE) {
if (resultCode == RESULT_OK) {
String barcode = intent.getStringExtra("SCAN_RESULT");
mBarcodeEdit.setText(barcode);
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
mFormatEdit.setText(format);
} else if (resultCode == RESULT_CANCELED) {
finish();
}
}
}
private static String validateFields(String barcode, String format,
String title, String price) {
StringBuilder errors = new StringBuilder();
if (barcode.matches("^\\s*$")) {
errors.append("Barcode required\n");
}
if (format.matches("^\\s*$")) {
errors.append("Format required\n");
}
if (title.matches("^\\s*$")) {
errors.append("Title required\n");
}
if (!price.matches("^-?\\d+(.\\d+)?$")) {
errors.append("Need numeric price\n");
}
return errors.toString();
}
}
有人有什麼錯誤?
10-17 10:21:37.746: D/AndroidRuntime(18643): Shutting down VM
10-17 10:21:37.746: W/dalvikvm(18643): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
10-17 10:21:37.746: E/AndroidRuntime(18643): Uncaught handler: thread main exiting due to uncaught exception
10-17 10:21:37.767: E/AndroidRuntime(18643): java.lang.NullPointerException
10-17 10:21:37.767: E/AndroidRuntime(18643): at java.math.BigDecimal.multiply(BigDecimal.java:1025)
10-17 10:21:37.767: E/AndroidRuntime(18643): at com.example.zxing_android_products.ProductDatabase.insert(ProductDatabase.java:69)
10-17 10:21:37.767: E/AndroidRuntime(18643): at com.example.zxing_android_products.AddProduct.onClick(AddProduct.java:74)
10-17 10:21:37.767: E/AndroidRuntime(18643): at android.view.View.performClick(View.java:2364)
10-17 10:21:37.767: E/AndroidRuntime(18643): at android.view.View.onTouchEvent(View.java:4179)
10-17 10:21:37.767: E/AndroidRuntime(18643): at android.widget.TextView.onTouchEvent(TextView.java:6541)
10-17 10:21:37.767: E/AndroidRuntime(18643): at android.view.View.dispatchTouchEvent(View.java:3709)
10-17 10:21:37.767: E/AndroidRuntime(18643): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-17 10:21:37.767: E/AndroidRuntime(18643): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-17 10:21:37.767: E/AndroidRuntime(18643): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-17 10:21:37.767: E/AndroidRuntime(18643): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-17 10:21:37.767: E/AndroidRuntime(18643): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-17 10:21:37.767: E/AndroidRuntime(18643): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
10-17 10:21:37.767: E/AndroidRuntime(18643): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
10-17 10:21:37.767: E/AndroidRuntime(18643): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
10-17 10:21:37.767: E/AndroidRuntime(18643): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
10-17 10:21:37.767: E/AndroidRuntime(18643): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
10-17 10:21:37.767: E/AndroidRuntime(18643): at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 10:21:37.767: E/AndroidRuntime(18643): at android.os.Looper.loop(Looper.java:123)
10-17 10:21:37.767: E/AndroidRuntime(18643): at android.app.ActivityThread.main(ActivityThread.java:4363)
10-17 10:21:37.767: E/AndroidRuntime(18643): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 10:21:37.767: E/AndroidRuntime(18643): at java.lang.reflect.Method.invoke(Method.java:521)
10-17 10:21:37.767: E/AndroidRuntime(18643): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-17 10:21:37.767: E/AndroidRuntime(18643): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-17 10:21:37.767: E/AndroidRuntime(18643): at dalvik.system.NativeStart.main(Native Method)
10-17 10:21:37.776: I/dalvikvm(18643): threadid=7: reacting to signal 3
10-17 10:21:37.776: E/dalvikvm(18643): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
10-17 10:21:39.636: I/Process(18643): Sending signal. PID: 18643 SIG: 9
我無法理解你的短信.. –
什麼需要改變?是否意味着ONE_HUNDRED變量不能爲空? – fewer
真的很抱歉再次問同樣的問題,因爲我無法理解你的評論。 您是否在問如何將值分配給ONE_HUNDRED變量或需要將哪個值分配給該變量? –