我有一个带有多个表的 SQLite DB,我想从其中一个表中读取所有内容,并使用它来填充列表,但我不知道为什么当我尝试从表中读取所有数据时应用程序崩溃.
这是我的代码:活动:
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class Manage_Groups extends ListActivity implements OnClickListener{
Button create_group_button;
DB_Handler db;
Groups_DB_Adapter groups_db;
ListView groupsList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.manage_groups);
// Initializing the buttons according to their ID
create_group_button = (Button)findViewById(R.id.create_group_button);
//Defines listeners for the buttons
create_group_button.setOnClickListener(this);
groups_db = new Groups_DB_Adapter(this);
// Opens the Main DB for read
db = new DB_Handler(this);
db.openToRead();
// Opens the Groups DB for read
groups_db.openToRead();
// This is where it crashes...
Cursor mCursor = groups_db.getAllGroups();
startManagingCursor(mCursor);
String[] from = new String[]{Groups_DB_Adapter.GROUP_NAME};
int[] to = {};
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,
mCursor, from, to = new int[] { android.R.id.text1 });
setListAdapter(cursorAdapter);
groupsList = getListView();
// Listens for a long click, and then pops up a delete dialog
groupsList.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
AlertDialog.Builder delete_group_alert = new AlertDialog.Builder(Manage_Groups.this);
delete_group_alert.setMessage("Are you sure you want to delete this group?");
final int positionToRemove = pos;
// No - closes the dialog and does nothing
delete_group_alert.setNegativeButton("No", null);
// Yes - Deletes the clicked group from the DB
delete_group_alert.setPositiveButton("Yes", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
groups_db.deleteGroup(positionToRemove);
}});
delete_group_alert.show();
return true;
}
});
}
//@Override
public void onClick(View src) {
Intent i;
switch (src.getId())
{
case R.id.create_group_button:
finish();
i = new Intent(this, Create_Group.class);
startActivity(i);
break;
}
}
}
组适配器:
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;
import android.util.Log;
public class Groups_DB_Adapter {
// A reference to the database used by this application/object
private SQLiteDatabase db;
private OpenHelper db_open_helper;
// Groups table
static final String GROUPS_TABLE_NAME = "groups_table";
static final String GROUP_ID = "group_id";
static final String GROUP_NAME = "group_name";
// The Activity or Application that is creating an object from this class
private final Context context;
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DB_Handler.DATABASE_NAME, null, DB_Handler.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public Groups_DB_Adapter(Context ctx) {
this.context = ctx;
}
// Opens the DB for write
public Groups_DB_Adapter openToWrite() throws SQLException {
this.db_open_helper = new OpenHelper(this.context);
this.db = this.db_open_helper.getWritableDatabase();
return this;
}
// Opens the DB for read
public Groups_DB_Adapter openToRead() throws SQLException {
this.db_open_helper = new OpenHelper(this.context);
this.db = this.db_open_helper.getReadableDatabase();
return this;
}
// Closes the DB
public void close() {
this.db_open_helper.close();
}
// Adds a new group
public void addNewGroup(String groupName)
{
// this is a key value pair holder used by android's SQLite functions
ContentValues values = new ContentValues();
values.put(GROUP_NAME, groupName);
// ask the database object to insert the new data
try{db.insert(GROUPS_TABLE_NAME, null, values);}
catch(Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
// Deletes a specific group
public boolean deleteGroup(long rowId) {
return this.db.delete(GROUPS_TABLE_NAME, GROUP_ID + "=" + rowId, null) > 0;
}
// Returns a Cursor with all the groups
public Cursor getAllGroups() {
return this.db.query(GROUPS_TABLE_NAME, new String[] { GROUP_ID,
GROUP_NAME }, null, null, null, null, null);
}
// Returns a specific group
public Cursor getGroup(long rowId) throws SQLException {
Cursor mCursor =
this.db.query(true, GROUPS_TABLE_NAME, new String[] { GROUP_ID, GROUP_NAME},
GROUP_ID + "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
// Updates a group
public boolean updateGroup(long rowId, String name){
ContentValues args = new ContentValues();
args.put(GROUP_NAME, name);
return this.db.update(GROUPS_TABLE_NAME, args, GROUP_ID + "=" + rowId, null) >0;
}
}
主数据库处理程序:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class DB_Handler {
// The database's name and version
public static final String DATABASE_NAME = "my_db";
public static final int DATABASE_VERSION = 2;
// A reference to the database used by this application/object
static SQLiteDatabase db;
private OpenHelper db_open_helper;
// The Activity or Application that is creating an object from this class
private static Context context;
// Contacts table
static final String CONTACTS_TABLE_NAME = "contacts_table";
static final String CONTACT_PHONE_NUMBER = "contact_phone_number";
static final String CONTACT_NAME = "contact_name";
private static final String CONTACTS_TABLE_CREATE = "CREATE TABLE " + CONTACTS_TABLE_NAME +
" (" +
CONTACT_PHONE_NUMBER + " INTEGER, " +
CONTACT_NAME + " TEXT);";
// Groups table
static final String GROUPS_TABLE_NAME = "groups_table";
static final String GROUP_ID = "group_id";
static final String GROUP_NAME = "group_name";
private static final String GROUPS_TABLE_CREATE = "CREATE TABLE " + GROUPS_TABLE_NAME +
" (" +
GROUP_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
GROUP_NAME + " TEXT);";
// Contacts <-> Groups Relations table
static final String CONTACT2GROUP_TABLE_NAME = "contact2group_table";
private static final String CONTACT2GROUP_TABLE_CREATE = "CREATE TABLE " + CONTACT2GROUP_TABLE_NAME +
" (" +
CONTACT_PHONE_NUMBER + " INTEGER, " +
GROUP_ID + " INTEGER);";
// Constructor to simplify Business logic access to the repository
public DB_Handler(Context context) {
DB_Handler.context = context;
}
public DB_Handler openToRead() throws android.database.SQLException {
db_open_helper = new OpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
db = db_open_helper.getReadableDatabase();
return this;
}
public DB_Handler openToWrite() throws android.database.SQLException {
db_open_helper = new OpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
db = db_open_helper.getWritableDatabase();
return this;
}
public void close(){
db_open_helper.close();
}
public class OpenHelper extends SQLiteOpenHelper {
public OpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creates the DB
@Override
public void onCreate(SQLiteDatabase db) {
// Creating the Contacts table
db.execSQL(CONTACTS_TABLE_CREATE);
// Creating the Groups table
db.execSQL(GROUPS_TABLE_CREATE);
// Creating the Contact2Group table
db.execSQL(CONTACT2GROUP_TABLE_CREATE);
}
// Upgrades the DB if a newer version is available
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + CONTACTS_TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + GROUPS_TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + CONTACT2GROUP_TABLE_NAME);
onCreate(db);
}
}
}
编辑:添加了日志 这是日志:
07-02 17:05:35.340: E/AndroidRuntime(1281): FATAL EXCEPTION: main
07-02 17:05:35.340: E/AndroidRuntime(1281): java.lang.RuntimeException: Unable to start activity ComponentInfo{meetapp.pack/meetapp.pack.Manage_Groups}: android.database.sqlite.SQLiteException: no such table: groups_table: , while compiling: SELECT group_id, group_name FROM groups_table
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.os.Handler.dispatchMessage(Handler.java:99)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.os.Looper.loop(Looper.java:137)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-02 17:05:35.340: E/AndroidRuntime(1281): at java.lang.reflect.Method.invokeNative(Native Method)
07-02 17:05:35.340: E/AndroidRuntime(1281): at java.lang.reflect.Method.invoke(Method.java:511)
07-02 17:05:35.340: E/AndroidRuntime(1281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-02 17:05:35.340: E/AndroidRuntime(1281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-02 17:05:35.340: E/AndroidRuntime(1281): at dalvik.system.NativeStart.main(Native Method)
07-02 17:05:35.340: E/AndroidRuntime(1281): Caused by: android.database.sqlite.SQLiteException: no such table: groups_table: , while compiling: SELECT group_id, group_name FROM groups_table
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1449)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1485)
07-02 17:05:35.340: E/AndroidRuntime(1281): at meetapp.pack.Groups_DB_Adapter.getAllGroups(Groups_DB_Adapter.java:127)
07-02 17:05:35.340: E/AndroidRuntime(1281): at meetapp.pack.Manage_Groups.onCreate(Manage_Groups.java:53)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.app.Activity.performCreate(Activity.java:4465)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-02 17:05:35.340: E/AndroidRuntime(1281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-02 17:05:35.340: E/AndroidRuntime(1281): ... 11 more
有任何想法吗?
谢谢!!