嗨,我在我的活动中创建了一个列表视图,我想在其中显示数据库中的内容。但是发生了一个错误,说没有找到列表视图有人可以给我一个更好的解决方案来克服这个错误吗?
我的 logcat 在下面给出
08-23 07:04:28.025: E/AndroidRuntime(24675): FATAL EXCEPTION: main
08-23 07:04:28.025: E/AndroidRuntime(24675): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.neochat/com.neochat.Friends_list}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
08-23 07:04:28.025: E/AndroidRuntime(24675): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-23 07:04:28.025: E/AndroidRuntime(24675): at
android.app.ActivityThread.startActivityNow(ActivityThread.java:2023)
08-23 07:04:28.025: E/AndroidRuntime(24675): at
android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
08-23 07:04:28.025: E/AndroidRuntime(24675): at
android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
08-23 07:04:28.025: E/AndroidRuntime(24675): at
android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749
08-23 07:04:28.025: E/AndroidRuntime(24675): at
android.widget.TabHost.setCurrentTab(TabHost.java:413)
08-23 07:04:28.025: E/AndroidRuntime(24675): at
android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
08-23 07:04:28.025: E/AndroidRuntime(24675): at
android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
08-23 07:04:28.025: E/AndroidRuntime(24675): at
android.view.View.performClick(View.java:4204)
08-23 07:04:28.025: E/AndroidRuntime(24675): at
android.view.View$PerformClick.run(View.java:17355)
08-23 07:04:28.025: E/AndroidRuntime(24675): at
android.os.Handler.handleCallback(Handler.java:725)
08-23 07:04:28.025: E/AndroidRuntime(24675): at
android.os.Handler.dispatchMessage(Handler.java:92)
08-23 07:04:28.025: E/AndroidRuntime(24675): at
android.os.Looper.loop(Looper.java:137
08-23 07:04:28.025: E/AndroidRuntime(24675): at
android.app.ActivityThread.main(ActivityThread.java:5041)
08-23 07:04:28.025: E/AndroidRuntime(24675): at
java.lang.reflect.Method.invokeNative(Native Method)
08-23 07:04:28.025: E/AndroidRuntime(24675): at
java.lang.reflect.Method.invoke(Method.java:511)
08-23 07:04:28.025: E/AndroidRuntime(24675): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-23 07:04:28.025: E/AndroidRuntime(24675): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-23 07:04:28.025: E/AndroidRuntime(24675): at dalvik.system.NativeStart.main(Native Method)
08-23 07:04:28.025: E/AndroidRuntime(24675): Caused by: java.lang.RuntimeException: Your
content must have a ListView whose id attribute is 'android.R.id.list'
08-23 07:04:28.025: E/AndroidRuntime(24675): at
android.app.ListActivity.onContentChanged(ListActivity.java:243)
在下面给出我的java类
public class Friends_list extends ListActivity implements
OnItemClickListener{
private ArrayList<String> results = new ArrayList<String>();
private String tableName = LoginDataBaseAdapter.tableName;
private SQLiteDatabase newDB;
ListView listview;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friends);
//listview=(ListView)findViewById(R.id.list);
openAndQueryDatabase();
displayResultList();
context=this;
//String[] arrayColumns=new String[]{"Name,Username"};
//int[]arrayViewIDs=new int[]
{R.id.textViewSMSSender,R.id.textViewMessageBody};
//Cursor cursor;
//cursor.getContentResolver().query(Uri.parse("content://sms/inbox"),
null,null,null,null);
//@SuppressWarnings("deprecation")
//SimpleCursorAdapter adapter=new
SimpleCursorAdapter(this,R.layout.friendsms, cursor, arrayColumns,
arrayViewIDs);
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_activated_1, android.R.id.text1,
value);
//listview.setAdapter(adapter);
//listview.setOnItemClickListener(this);
}
private void setListAdapter(ArrayAdapter<String> arrayAdapter) {
// TODO Auto-generated method stub
}
private void openAndQueryDatabase() {
try {
LoginDataBaseAdapter loginDataBaseAdapter=new
LoginDataBaseAdapter(this.getApplicationContext());
newDB=loginDataBaseAdapter.getWritableDatabase();
Cursor c=newDB.rawQuery(" SELECT NAME ,
USERNAME FROM "+ tableName, null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String Name=c.getString(c.getColumnIndex("NAME"));
String Username=c.getString(c.getColumnIndex("USERNAME"));
}
while(c.moveToNext());
}
}
}
catch (SQLiteException se) {
Log.e(getClass().getSimpleName(), "Could not open the database");
}
finally {
if(newDB!=null)
newDB.execSQL(" DELETE FROM "+ tableName);
newDB.close();
}
}
private void displayResultList() {
TextView tView = new TextView(this);
tView.setText("Friends");
getListView().addHeaderView(tView);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
getListView().setTextFilterEnabled(true);
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
Intent in2=new
Intent(Friends_list.this,InboxActivity.class);
startActivity(in2);
}
}
这是我用于显示列表视图的 xml 代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ListView
android:id="@+id/list"
style="@style/WhiteText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
>
</ListView>
</LinearLayout>