我正在尝试通过使用游标从 SQLite 获取数据来填充列表视图。但每次应用程序突然关闭时显示“不幸停止”
这是简单的代码:
public class MainActivity extends ListActivity {
//Android listview object
ListView listViewPhoneBook;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listContent = (ListView) findViewById(R.id.listPhoneBook);
DbAdapter mDb = new DbAdapter(this);
mDb.open();
Cursor cursor =mDb.fetchAllPrograms();
startManagingCursor(cursor);
String[] from = new String[] { DbAdapter.KEY_TITLE };
int[] to = new int[] { R.id.textViewName };
SimpleCursorAdapter cursorAdapter = new
SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
//Onclick ListView setlistener
listContent.setTextFilterEnabled(true);
listContent.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
}
XML 文件:Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/listPhoneBook"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="0.1dp"
android:divider="#0000CC"
>
</ListView>
</LinearLayout>
行.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textViewName"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#0000FF"
android:text="TextView" />
</LinearLayout>
数据库文件:DbAdapter.java
public class DbAdapter{
public final static String KEY_TITLE = "title";
public static final String KEY_BODY = "body";
public static final String KEY_ROWID = "_id";
private static final String TAG = "LogoDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
// Database creation sql statement
private static final String DATABASE_NAME = "Logo";
private static final String DATABASE_TABLE = "LogoProgram";
private static final int DATABASE_VERSION = 1;
private final Context mCtx;
private static final String TABLE_CREATE =
"CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
KEY_TITLE + " TEXT, " +
KEY_BODY + " TEXT);";
@SuppressLint("SdCardPath")
private String DATA_PATH="/data/data/com.example/Logo";
private static class DatabaseHelper extends SQLiteOpenHelper{
// Initialize context or database to be used as context
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
}
//In case of updated version of database is available fire the query
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS Logoprogram");
onCreate(db);
}
}
public DbAdapter(Context ctx) {
mCtx = ctx;
}
public DbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public long createProgram(String title, String body) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_BODY, body);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
public String getdata() {
String[] columns= new String[]{ KEY_ROWID, KEY_TITLE,KEY_BODY};
Cursor c= mDb.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result="";
int id=c.getColumnIndex(KEY_ROWID);
int iTitle=c.getColumnIndex(KEY_TITLE);
int iBody=c.getColumnIndex(KEY_BODY);
for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
result= result + c.getString(id) + " " + c.getString(iTitle) +
" " + c.getString(iBody) + "\n";
//result= result + c.getString(iTitle) + "\n";
}
return result;
}
public Cursor fetchAllPrograms() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
KEY_BODY}, null, null, null, null, null);
}
public Cursor fetchPrograms(long rowId) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_TITLE, KEY_BODY}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}}
游标方法存在弃用警告。谁能告诉我,我哪里出错了?提前致谢!
日志如下:
03-03 22:42:46.695: D/AndroidRuntime(555): Shutting down VM
03-03 22:42:46.695: W/dalvikvm(555): threadid=1: thread exiting with uncaught
exception (group=0x409961f8)
03-03 22:42:46.727: E/AndroidRuntime(555): FATAL EXCEPTION: main
03-03 22:42:46.727: E/AndroidRuntime(555): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.android.listview/com.android.listview.MainActivity}:
java.lang.RuntimeException: Your content must have a ListView whose id attribute is
'android.R.id.list'
03-03 22:42:46.727: E/AndroidRuntime(555): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
03-03 22:42:46.727: E/AndroidRuntime(555): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
03-03 22:42:46.727: E/AndroidRuntime(555): at
android.app.ActivityThread.access$600(ActivityThread.java:122)
03-03 22:42:46.727: E/AndroidRuntime(555): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
03-03 22:42:46.727: E/AndroidRuntime(555): at
android.os.Handler.dispatchMessage(Handler.java:99)
03-03 22:42:46.727: E/AndroidRuntime(555): at android.os.Looper.loop(Looper.java:137)
03-03 22:42:46.727: E/AndroidRuntime(555): at
android.app.ActivityThread.main(ActivityThread.java:4340)
03-03 22:42:46.727: E/AndroidRuntime(555): at
java.lang.reflect.Method.invokeNative(Native Method)
03-03 22:42:46.727: E/AndroidRuntime(555): at
java.lang.reflect.Method.invoke(Method.java:511)
03-03 22:42:46.727: E/AndroidRuntime(555): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-03 22:42:46.727: E/AndroidRuntime(555): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-03 22:42:46.727: E/AndroidRuntime(555): at dalvik.system.NativeStart.main(Native
Method)
03-03 22:42:46.727: E/AndroidRuntime(555): Caused by: java.lang.RuntimeException: Your
content must have a ListView whose id attribute is 'android.R.id.list'
03-03 22:42:46.727: E/AndroidRuntime(555): at
android.app.ListActivity.onContentChanged(ListActivity.java:243)
03-03 22:42:46.727: E/AndroidRuntime(555): at
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
03-03 22:42:46.727: E/AndroidRuntime(555): at
android.app.Activity.setContentView(Activity.java:1835)
03-03 22:42:46.727: E/AndroidRuntime(555): at
com.android.listview.MainActivity.onCreate(MainActivity.java:24)
03-03 22:42:46.727: E/AndroidRuntime(555): at
android.app.Activity.performCreate(Activity.java:4465)
03-03 22:42:46.727: E/AndroidRuntime(555): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-03 22:42:46.727: E/AndroidRuntime(555): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
03-03 22:42:46.727: E/AndroidRuntime(555): ... 11 more
03-03 22:42:49.625: I/Process(555): Sending signal. PID: 555 SIG: 9