我在不同的类中在 android 中创建了一个数据库和一个列表。当通过单击添加按钮添加项目时,将其添加到列表和数据库中。但是,当单击添加按钮时,它会崩溃。
这是列表类
package bookshelf.Android.Java;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import bookshelf.Android.Java.R;
public class Own extends Activity
{
private EditText item;
private ListView lv;
private Toast toast;
private Button addButton;
ArrayList<String> items;
ArrayAdapter<String> listad;
List<String> books;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.own);
CharSequence text = "Item added!";
toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
setUpView();
}
private void setUpView()
{
item = (EditText)this.findViewById(R.id.txtAmount);
lv = (ListView)this.findViewById(R.id.listView1);
addButton = (Button)this.findViewById(R.id.Add);
items = new ArrayList<String>();
items.clear();
final DataBaseOwn dbo = new DataBaseOwn(Own.this);
listad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);//need to create a way to get items to equal books
lv.setAdapter(listad);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
addItemList();
books = dbo.get(items);
toast.show();
}
});
item.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event)
{
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_ENTER)
{
addItemList();
}
return true;
}
});
}
protected void addItemList()
{
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if (isInputValid(item))
{
items.add(0,item.getText().toString());
item.setText("");
listad.notifyDataSetChanged();
}
}
protected boolean isInputValid(EditText item2)
{
// TODO Auto-generatd method stub
if (item2.getText().toString().trim().length()<1)
{
item2.setError("Please Enter Item");
return false;
}
else
{
return true;
}
}
}
这是我创建的数据库:
package bookshelf.Android.Java;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseOwn extends SQLiteOpenHelper
{
public static final int VERSION = 1;
public static final String TABLE_NAME = "OwnList";
public static final String DBNAME = "ownList.sqlite";
public static final String ID = "id";
public static final String BOOK = "book";
static SQLiteDatabase db;
public DataBaseOwn(Context context)
{
super(context, DBNAME, null, VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
createDatabase(db);
}
private void createDatabase(SQLiteDatabase db)
{
db.execSQL("create table " + TABLE_NAME + "(" + ID + "integer primary key autoincrement not null" + BOOK + " text " + ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public Long Insert(String book)
{
ContentValues values = new ContentValues();
values.put(BOOK, book);
return db.insert(TABLE_NAME, null, values);
}
public List<String> get( ArrayList<String> item)
{
db = this.getWritableDatabase();
String[] column = new String[] {BOOK};
Cursor c = db.query(TABLE_NAME, column, null, null, null, null, null);
List<String> result = item;
int columnIndex = c.getColumnIndex(BOOK);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
result.add(c.getString(columnIndex));
}
return result;
}
}
这些是我得到的错误:
10-17 04:42:47.764: W/dalvikvm(2247): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
10-17 04:42:47.893: E/AndroidRuntime(2247): FATAL EXCEPTION: main
10-17 04:42:47.893: E/AndroidRuntime(2247): android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY: , while compiling: create table OwnList(idinteger primary key autoincrement not nullbook text );
10-17 04:42:47.893: E/AndroidRuntime(2247): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
10-17 04:42:47.893: E/AndroidRuntime(2247): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
10-17 04:42:47.893: E/AndroidRuntime(2247): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:134)
10-17 04:42:47.893: E/AndroidRuntime(2247): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
10-17 04:42:47.893: E/AndroidRuntime(2247): at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
10-17 04:42:47.893: E/AndroidRuntime(2247): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
10-17 04:42:47.893: E/AndroidRuntime(2247): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1899)
10-17 04:42:47.893: E/AndroidRuntime(2247): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1839)
10-17 04:42:47.893: E/AndroidRuntime(2247): at bookshelf.Android.Java.DataBaseOwn.createDatabase(DataBaseOwn.java:36)
10-17 04:42:47.893: E/AndroidRuntime(2247): at bookshelf.Android.Java.DataBaseOwn.onCreate(DataBaseOwn.java:32)
10-17 04:42:47.893: E/AndroidRuntime(2247): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:165)
10-17 04:42:47.893: E/AndroidRuntime(2247): at bookshelf.Android.Java.DataBaseOwn.get(DataBaseOwn.java:54)
10-17 04:42:47.893: E/AndroidRuntime(2247): at bookshelf.Android.Java.Own$1.onClick(Own.java:54)
10-17 04:42:47.893: E/AndroidRuntime(2247): at android.view.View.performClick(View.java:3511)
10-17 04:42:47.893: E/AndroidRuntime(2247): at android.view.View$PerformClick.run(View.java:14105)
10-17 04:42:47.893: E/AndroidRuntime(2247): at android.os.Handler.handleCallback(Handler.java:605)
10-17 04:42:47.893: E/AndroidRuntime(2247): at android.os.Handler.dispatchMessage(Handler.java:92)
10-17 04:42:47.893: E/AndroidRuntime(2247): at android.os.Looper.loop(Looper.java:137)
10-17 04:42:47.893: E/AndroidRuntime(2247): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-17 04:42:47.893: E/AndroidRuntime(2247): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 04:42:47.893: E/AndroidRuntime(2247): at java.lang.reflect.Method.invoke(Method.java:511)
10-17 04:42:47.893: E/AndroidRuntime(2247): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-17 04:42:47.893: E/AndroidRuntime(2247): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-17 04:42:47.893: E/AndroidRuntime(2247): at dalvik.system.NativeStart.main(Native Method)