我已经在logcat中暂时出现这个错误了一段时间。我尝试了很多东西,比如清理我的应用程序中的数据,卸载并重新安装我的应用程序,升级我的数据库版本,我甚至删除了我的项目并创建了一个新项目,但每次我运行我的应用程序时总是会遇到此错误。有人可以帮助我。
我想要做的是在活动添加项目和查看项目中有两个按钮,每次我点击查看项目按钮我都会收到此错误。
单击视图按钮时,它必须在列表视图中显示我保存在数据库中的数据,但这不会发生,因为我的应用程序始终停止并显示在我的logcat中。
This is the Image of the activity where the data should display
这是我的logcat:
02-09 05:24:23.850: E/AndroidRuntime(8037): at com.system.inventorysystem.ViewProductItem.onCreate(ViewProductItem.java:46)
02-09 05:46:59.810: E/AndroidRuntime(13150): FATAL EXCEPTION: main
02-09 05:46:59.810: E/AndroidRuntime(13150): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.system.inventorysystem/com.system.inventorysystem.ViewProductItem}: android.database.sqlite.SQLiteException: no such table: ItemTable: , while compiling: SELECT _id, ItemDescription, ItemQuantity, ItemUnit, ItemPrice FROM ItemTable
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.app.ActivityThread.access$600(ActivityThread.java:122)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.os.Handler.dispatchMessage(Handler.java:99)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.os.Looper.loop(Looper.java:137)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.app.ActivityThread.main(ActivityThread.java:4340)
02-09 05:46:59.810: E/AndroidRuntime(13150): at java.lang.reflect.Method.invokeNative(Native Method)
02-09 05:46:59.810: E/AndroidRuntime(13150): at java.lang.reflect.Method.invoke(Method.java:511)
02-09 05:46:59.810: E/AndroidRuntime(13150): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-09 05:46:59.810: E/AndroidRuntime(13150): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-09 05:46:59.810: E/AndroidRuntime(13150): at dalvik.system.NativeStart.main(Native Method)
02-09 05:46:59.810: E/AndroidRuntime(13150): Caused by: android.database.sqlite.SQLiteException: no such table: ItemTable: , while compiling: SELECT _id, ItemDescription, ItemQuantity, ItemUnit, ItemPrice FROM ItemTable
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1449)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1485)
02-09 05:46:59.810: E/AndroidRuntime(13150): at com.system.inventorysystem.DBAdapter.getAllItemProduct(DBAdapter.java:108)
02-09 05:46:59.810: E/AndroidRuntime(13150): at com.system.inventorysystem.ViewProductItem.onCreate(ViewProductItem.java:48)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.app.Activity.performCreate(Activity.java:4465)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-09 05:46:59.810: E/AndroidRuntime(13150): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-09 05:46:59.810: E/AndroidRuntime(13150): ... 11 more
这是我的数据库适配器。
BDAdapter.java
package com.system.inventorysystem;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBAdapter {
private static final String DATABASE_NAME = "ProductItemdb";
private static final int DATABASE_VERSION = 4;
private static final String TABLE_PRODUCTITEM = "ItemTable";
public static final String KEY_ItemID = "_id";
public static final String KEY_ITEM_DESCRIPTION = "ItemDescription";
public static final String KEY_ITEM_QUANTITY = "ItemQuantity";
public static final String KEY_ITEM_UNIT = "ItemUnit";
public static final String KEY_ITEM_PRICE = "ItemPrice";
public static final String KEY_ITEM_DATE = "Date/Time";
private static final String PRODUCTITEM_TABLE = "create table ItemTable (_id integer primary key autoincrement, "
+ " ItemDescription text not null, ItemQuantity integer not null, ItemUnit text not null, ItemPrice float not null, Date/Time datetime DEFAULT CURRENT_TIMESTAMP);";
private final Context ourContext;
private DatabaseHelper ourHelper;
private SQLiteDatabase ourDatabase;
public DBAdapter(Context con) {
this.ourContext = con;
ourHelper = new DatabaseHelper(ourContext);
}
static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try {
db.execSQL(PRODUCTITEM_TABLE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTITEM);
onCreate(db);
}
}
//open Database
public DBAdapter open() throws SQLException {
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
//close database
public void close() {
ourHelper.close();
}
//METHODS FOR ITEM PRODUCTS
//insert data
public long addItemProduct(String description, int quantity, String unit, Float price)
{
ContentValues cv = new ContentValues();
cv.put(KEY_ITEM_DESCRIPTION, description);
cv.put(KEY_ITEM_QUANTITY, quantity);
cv.put(KEY_ITEM_UNIT, unit);
cv.put(KEY_ITEM_PRICE, price);
return ourDatabase.insert(TABLE_PRODUCTITEM, null, cv);
}
//update data
public boolean updateItemProduct(long itemId, String description, int quantity, String unit, Float price)
{
ContentValues cv = new ContentValues();
cv.put(KEY_ITEM_DESCRIPTION, itemId);
cv.put(KEY_ITEM_QUANTITY, quantity);
cv.put(KEY_ITEM_UNIT, unit);
cv.put(KEY_ITEM_PRICE, price);
return ourDatabase.update(TABLE_PRODUCTITEM, cv, KEY_ItemID + "=" + itemId, null) > 0;
}
//delete data
public boolean deleteItemProduct(long itemId)
{
return ourDatabase.delete(TABLE_PRODUCTITEM, KEY_ItemID + "=" + itemId, null) > 0;
}
//retrieve data
public Cursor getItemProduct(long itemId) throws SQLException
{
Cursor mCursor = ourDatabase.query(true, TABLE_PRODUCTITEM, new String[] {KEY_ItemID, KEY_ITEM_DESCRIPTION,
KEY_ITEM_QUANTITY, KEY_ITEM_UNIT, KEY_ITEM_PRICE}, KEY_ItemID + "=" + itemId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//retrieve all data
public Cursor getAllItemProduct()
{
return ourDatabase.query(TABLE_PRODUCTITEM, new String[] {KEY_ItemID, KEY_ITEM_DESCRIPTION, KEY_ITEM_QUANTITY, KEY_ITEM_UNIT, KEY_ITEM_PRICE},
null, null, null, null, null);
}
}
这是我的java类,我试图在我的数据库中显示listview中的数据。
ViewItemProduct.java
package com.system.inventorysystem;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class ViewProductItem extends Activity implements View.OnClickListener{
DBAdapter ourDatabase;
Typeface customFont;
TextView view_item_txt;
Button delete,update;
ListView myList;
@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.viewproductitem);
customFont = Typeface.createFromAsset(getAssets(), "fonts/EraserRegular.ttf");
view_item_txt = (TextView)findViewById(R.id.view_item_tv);
view_item_txt.setTypeface(customFont);
delete = (Button)findViewById(R.id.view_delete_button);
delete.setTypeface(customFont);
delete.setOnClickListener(this);
update = (Button)findViewById(R.id.view_update_button);
update.setTypeface(customFont);
update.setOnClickListener(this);
openDB();
ourDatabase.open();
final Cursor c = ourDatabase.getAllItemProduct();
String[] ViewItemsNames = new String[] {DBAdapter.KEY_ItemID,DBAdapter.KEY_ITEM_DESCRIPTION,DBAdapter.KEY_ITEM_QUANTITY,DBAdapter.KEY_ITEM_UNIT,DBAdapter.KEY_ITEM_PRICE};
int[] ViewItemsID = new int[] {R.id.view_item_id,R.id.view_item_desc,R.id.view_item_quantity,R.id.view_item_unit,R.id.view_item_price};
final SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.layout.viewproduct_items, c, ViewItemsNames, ViewItemsID,0);
myList = (ListView)findViewById(R.id.view_item_listview);
myList.setAdapter(myCursorAdapter);
}
private void openDB() {
ourDatabase = new DBAdapter(this);
ourDatabase.open();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.view_delete_button:
Intent openDeleteItem = new Intent(this,DeleteProductItem.class);
this.startActivity(openDeleteItem);
break;
case R.id.view_update_button:
Intent openUpdateItem = new Intent(this,UpdateProductItem.class);
this.startActivity(openUpdateItem);
break;
}
}
}
这是我的xml文件。
viewitemproduct.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/greenboard_background" >
<TextView
android:id="@+id/view_item_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:text="@string/view_item"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="35sp" />
<ListView
android:id="@+id/view_item_listview"
android:layout_width="543dp"
android:layout_height="695dp"
android:layout_below="@+id/view_item_tv"
android:layout_centerHorizontal="true" >
</ListView>
<Button
android:id="@+id/view_delete_button"
android:layout_width="260dp"
android:layout_height="70dp"
android:layout_alignRight="@+id/view_item_listview"
android:layout_below="@+id/view_item_listview"
android:layout_marginTop="10dp"
android:text="@string/delete_item"
android:textSize="40sp" />
<Button
android:id="@+id/view_update_button"
android:layout_width="260dp"
android:layout_height="70dp"
android:layout_alignBaseline="@+id/view_delete_button"
android:layout_alignBottom="@+id/view_delete_button"
android:layout_alignLeft="@+id/view_item_listview"
android:text="@string/update_item"
android:textSize="40sp" />
</RelativeLayout>
这是我的Listview中项目的texviews。
viewproduct_items.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/view_item_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_alignParentTop="true"
android:text="@string/itemno"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp" />
<TextView
android:id="@+id/view_item_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/item_desc"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp" />
<TextView
android:id="@+id/view_item_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_below="@+id/view_item_desc"
android:text="@string/item_quantity"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp" />
<TextView
android:id="@+id/view_item_unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/view_item_quantity"
android:layout_alignBottom="@+id/view_item_quantity"
android:layout_centerHorizontal="true"
android:text="@string/item_unit"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp" />
<TextView
android:id="@+id/view_item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_below="@+id/view_item_unit"
android:text="@string/item_price"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp" />
</RelativeLayout>
答案 0 :(得分:1)
您的- (IBAction)tableviewapicall:(id)sender
{
if(tableviewbool)
{
_tableview.hidden=NO;
tableviewbool=NO;
[_tableview superview];
}
else
{
_tableview.hidden=YES;
tableviewbool=YES;
}
}
存在语法问题。 create table
不是有效的列名 - 摆脱Date/Time
。
由于/
try
中的catch
- SQLiteOpenHelper
,语法问题已隐藏。如果出现问题,onCreate()
必须抛出异常。否则onCreate()
认为一切正常。
解决上述两个问题后,请先卸载您的应用/清除其数据或提升数据库版本,以便再次调用SQLiteOpenHelper
。
答案 1 :(得分:0)
您的create table语句的格式不正确。
所以删除&#34; /&#34;来自表列名称中的日期/时间。
它将解决问题。
实际问题是由于此问题而未创建表,并且由于try catch块而无法抛出异常。