我按下TestDBIIIIActivity类中的按钮show来改变从main到showdata的界面。和 弹出窗口显示强制关闭。
来自LogCat
01-11 16:39:43.434: E/AndroidRuntime(13835): FATAL EXCEPTION: main
01-11 16:39:43.434: E/AndroidRuntime(13835): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.testdbiiii/com.app.testdbiiii.ShowData}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-11 16:39:43.434: E/AndroidRuntime(13835): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-11 16:39:43.434: E/AndroidRuntime(13835): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-11 16:39:43.434: E/AndroidRuntime(13835): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-11 16:39:43.434: E/AndroidRuntime(13835): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-11 16:39:43.434: E/AndroidRuntime(13835): at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 16:39:43.434: E/AndroidRuntime(13835): at android.os.Looper.loop(Looper.java:123)
01-11 16:39:43.434: E/AndroidRuntime(13835): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-11 16:39:43.434: E/AndroidRuntime(13835): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 16:39:43.434: E/AndroidRuntime(13835): at java.lang.reflect.Method.invoke(Method.java:507)
01-11 16:39:43.434: E/AndroidRuntime(13835): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-11 16:39:43.434: E/AndroidRuntime(13835): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-11 16:39:43.434: E/AndroidRuntime(13835): at dalvik.system.NativeStart.main(Native Method)
01-11 16:39:43.434: E/AndroidRuntime(13835): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-11 16:39:43.434: E/AndroidRuntime(13835): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
01-11 16:39:43.434: E/AndroidRuntime(13835): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210)
01-11 16:39:43.434: E/AndroidRuntime(13835): at android.app.Activity.setContentView(Activity.java:1657)
01-11 16:39:43.434: E/AndroidRuntime(13835): at com.app.testdbiiii.ShowData.onCreate(ShowData.java:27)
01-11 16:39:43.434: E/AndroidRuntime(13835): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-11 16:39:43.434: E/AndroidRuntime(13835): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-11 16:39:43.434: E/AndroidRuntime(13835): ... 11 more
来自Class TestDBIIIIActivity
public class TestDBIIIIActivity extends Activity {
Button add,show;
EditText name;
TextView time;
private DatabaseAdapter myDatabaseAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
add = (Button)findViewById(R.id.add);
show =(Button)findViewById(R.id.show);
name =(EditText)findViewById(R.id.name);
time =(TextView)findViewById(R.id.time);
myDatabaseAdapter = new DatabaseAdapter(this);
myDatabaseAdapter.openToWrite();
add.setOnClickListener(buttonAddOnClickListener);
show.setOnClickListener(buttonShowClickListener);
}
Button.OnClickListener buttonShowClickListener = new Button.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getBaseContext(), ShowData.class);
startActivity(intent);
}
};
Button.OnClickListener buttonAddOnClickListener = new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String data1 = name.getText().toString();
String data2 = time.getText().toString();
myDatabaseAdapter.insert(data1, data2);
}
};
}
来自ShowData数据
public class ShowData extends ListActivity {
Button refresh;
private DatabaseAdapter myDatabaseAdapter;
SimpleCursorAdapter cursorAdapter;
Cursor cursor;
ListView listContent;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showdata);
cursor = myDatabaseAdapter.queueAll();
refresh = (Button) findViewById(R.id.button1);
listContent = (ListView) findViewById(R.id.contentlist);
String[] from = new String[] { DatabaseAdapter.KEY_ID,DatabaseAdapter.KEY_CONTENT1, DatabaseAdapter.KEY_CONTENT2 };
int[] to = new int[] { R.id.id, R.id.text1, R.id.text2 };
cursorAdapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor,from, to);
listContent.setAdapter(cursorAdapter);
myDatabaseAdapter = new DatabaseAdapter(this);
refresh.setOnClickListener(buttonRefreshClickListener);
}
Button.OnClickListener buttonRefreshClickListener = new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
updateList();
}
};
private void updateList() {
cursor.requery();
}
}
来自Class DatabaseAdapter
public class DatabaseAdapter {
public static final String MYDATABASE_NAME = "MY_DATABASE";
public static final String MYDATABASE_TABLE = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_CONTENT1 = "Name";
public static final String KEY_CONTENT2 = "Time";
// create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE = "create table "+ MYDATABASE_TABLE + " (" + KEY_ID+ " integer primary key autoincrement, " + KEY_CONTENT1 + " text not null, " + KEY_CONTENT2 + " text not null);";
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public DatabaseAdapter(Context c) {
context = c;
}
public DatabaseAdapter openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public DatabaseAdapter openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
public void close() {
sqLiteHelper.close();
}
public long insert(String name, String time) {
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT1, name);
contentValues.put(KEY_CONTENT2, time);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
public int deleteAll() {
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}
public Cursor queueAll() {
String[] columns = new String[] { KEY_ID, KEY_CONTENT1, KEY_CONTENT2 };
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null,null, null, null, null);
return cursor;
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}
请帮帮我。
答案 0 :(得分:0)
尝试将listview ID用作
<Listview
android:id="@android:id/list"
./>
也可以使用
listcontent=getListView();
而不是
listContent = (ListView) findViewById(R.id.contentlist);
答案 1 :(得分:0)
您应该在布局文件中定义一个列表视图
android:id =“@ + id / contentlist”... /&gt;
或者你必须使用android.R.id.list
访问android listview