我按照示例代码但我无法创建我的数据库我不知道为什么,在文件夹中的模拟器中没有任何数据库。这是我的代码 这是我的主要活动
public class T5Compras extends Activity {
private EditText myEditText;
private ListView myListView;
private SimpleCursorAdapter cursorAdapter;
private Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_t5_compras);
final ComprasDBAdapter comprasDBAdapter = new ComprasDBAdapter(this);
myEditText = (EditText) findViewById(R.id.txtAdd);
myListView = (ListView) findViewById(R.id.lstBuying);
myEditText.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==KeyEvent.ACTION_DOWN){
if(keyCode==KeyEvent.KEYCODE_ENTER){
comprasDBAdapter.open();
comprasDBAdapter.insertProduct("jorge");
myEditText.setText("");
return true;
}
}
return false;
}
});
/* ComprasDBAdapter comprasDBAdapter = new ComprasDBAdapter(getApplicationContext());
comprasDBAdapter.open();
cursor = comprasDBAdapter.getAllProductItemsCursor();
cursorAdapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.lst_layout, cursor,
new String[]{ComprasDBAdapter.KEY_PRODUCT,ComprasDBAdapter.KEY_ID}, new int []{R.id.list_txtProduct,R.id.list_txtId });
*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.t5_compras, menu);
return true;
}
}
这是我的班级
public class ComprasDBAdapter {
private static final String DATABASE_NAME = "listCompra.db";
private static final String DATABASE_TABLE = "productItems";
private static final int DATABASE_VERSION = 1;
private SQLiteDatabase db;
public static final String KEY_ID = "_id";
public static final String KEY_PRODUCT = "product";
private ComprasDBOpenHelper dbHelper;
public ComprasDBAdapter(Context _context) {
// TODO Crear una instancia de ComprasDBOpenHelper.
// Inicializar el CursorFactory a null para utilizar el que
// el sistema tiene por defecto.
ComprasDBOpenHelper comprasDBOpenHelper = new ComprasDBOpenHelper(_context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void close() {
db.close();
}
public void open() throws SQLiteException {
try
{
db = dbHelper.getWritableDatabase();
}
catch (SQLiteException ex)
{
db = dbHelper.getReadableDatabase();
}
}
// Insertar un producto
public long insertProduct(String _product) {
// TODO
ContentValues cv = new ContentValues();
cv.put("KEY_PRODUCT", _product);
db.insert(DATABASE_TABLE, null, cv);
return 0;
}
// Eliminar un producto según su ID
public boolean removeProduct(int _id) {
//TODO
db.delete(DATABASE_TABLE, "KEY_ID='_id'", null);
return false;
}
// Actualizar el producto
public boolean updateProduct(long _rowIndex, String _product) {
// TODO
ContentValues cv = new ContentValues();
cv.put(KEY_PRODUCT, _product);
db.update(DATABASE_TABLE, cv, "KEY_PRODUCT='_product'", null);
return false;
}
public Cursor getAllProductItemsCursor() {
// TODO
return db.rawQuery("SELECT * FROM productItems", null);
}
private class ComprasDBOpenHelper extends SQLiteOpenHelper {
public ComprasDBOpenHelper(Context context,
String name,
CursorFactory factory,
int version)
{
super(context, name, factory, version);
}
// Sentencia SQL para crear la nueva base de datos.
private static final String DATABASE_CREATE = "CREATE TABLE "
+ DATABASE_TABLE + " ("
+ KEY_ID
+ " integer primary key autoincrement, "
+ KEY_PRODUCT
+ " text not null)";
@Override
public void onCreate(SQLiteDatabase _db)
{
_db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
{
Log.w("ComprasDBAdapter", "Upgrading from version " +
_oldVersion + " to " +
_newVersion + ", which will destroy all old data");
_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(_db);
}
}
}
当我尝试插入一些值时,我的应用程序崩溃了,logcat说错误就在那里我认为是数据库因为没有创建数据库
那是logcat
06-16 06:45:08.331: E/AndroidRuntime(1066): FATAL EXCEPTION: main
06-16 06:45:08.331: E/AndroidRuntime(1066): java.lang.NullPointerException
06-16 06:45:08.331: E/AndroidRuntime(1066): at com.imaginagroup.t5compras.ComprasDBAdapter.open(ComprasDBAdapter.java:43)
06-16 06:45:08.331: E/AndroidRuntime(1066): at com.imaginagroup.t5compras.T5Compras$1.onKey(T5Compras.java:43)
06-16 06:45:08.331: E/AndroidRuntime(1066): at android.view.View.dispatchKeyEvent(View.java:7200)
06-16 06:45:08.331: E/AndroidRuntime(1066): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
06-16 06:45:08.331: E/AndroidRuntime(1066): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
06-16 06:45:08.331: E/AndroidRuntime(1066): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
06-16 06:45:08.331: E/AndroidRuntime(1066): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
06-16 06:45:08.331: E/AndroidRuntime(1066): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1920)
06-16 06:45:08.331: E/AndroidRuntime(1066): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1395)
06-16 06:45:08.331: E/AndroidRuntime(1066): at android.app.Activity.dispatchKeyEvent(Activity.java:2370)
06-16 06:45:08.331: E/AndroidRuntime(1066): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1847)
06-16 06:45:08.331: E/AndroidRuntime(1066): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
06-16 06:45:08.331: E/AndroidRuntime(1066): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)
06-16 06:45:08.331: E/AndroidRuntime(1066): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818)
06-16 06:45:08.331: E/AndroidRuntime(1066): at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 06:45:08.331: E/AndroidRuntime(1066): at android.os.Looper.loop(Looper.java:137)
06-16 06:45:08.331: E/AndroidRuntime(1066): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-16 06:45:08.331: E/AndroidRuntime(1066): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 06:45:08.331: E/AndroidRuntime(1066): at java.lang.reflect.Method.invoke(Method.java:511)
06-16 06:45:08.331: E/AndroidRuntime(1066): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-16 06:45:08.331: E/AndroidRuntime(1066): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-16 06:45:08.331: E/AndroidRuntime(1066): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
从
更改ComprasDBAdapter构造函数public ComprasDBAdapter(Context _context) {
// TODO Crear una instancia de ComprasDBOpenHelper.
// Inicializar el CursorFactory a null para utilizar el que
// el sistema tiene por defecto.
ComprasDBOpenHelper comprasDBOpenHelper = new ComprasDBOpenHelper(_context, DATABASE_NAME, null, DATABASE_VERSION);
}
到
public ComprasDBAdapter(Context _context) {
// TODO Crear una instancia de ComprasDBOpenHelper.
// Inicializar el CursorFactory a null para utilizar el que
// el sistema tiene por defecto.
dbHelper = new ComprasDBOpenHelper(_context, DATABASE_NAME, null, DATABASE_VERSION);
}