您好我在我的活动中创建了一个列表视图,我想在其中显示数据库中的内容。但是发生了一个错误,说没有找到列表视图有人可以给我一个更好的解决方案来克服这个错误吗?
我的日志猫如下
08-21 06:54:52.103: E/AndroidRuntime(6874): FATAL EXCEPTION: main
08-21 06:54:52.103: E/AndroidRuntime(6874): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.neochat/com.neochat.Friends}: 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-21 06:54:52.103: E/AndroidRuntime(6874): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-21 06:54:52.103: E/AndroidRuntime(6874): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-21 06:54:52.103: E/AndroidRuntime(6874): at android.app.ActivityThread.access$600(ActivityThread.java:141)
我正在为下面的 listview 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);
openAndQueryDatabase();
displayResultList();
setContentView(R.layout.activity_friends);
context=this;
listview=(ListView)findViewById(R.id.listView1);
//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);
//listview.setAdapter(adapter);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_activated_1, android.R.id.text1,value);
listview.setOnItemClickListener(this);
}
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);
}
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();
}
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent in2=new Intent(Friends_list.this,InboxActivity.class);
startActivity(in2);
}
}
这是我创建数据库的代码
public class LoginDataBaseAdapter extends SQLiteOpenHelper {
public static final int NAME_COLUMN=2;
static final String DATABASE_NAME = "UserDetails.db";
static final int DATABASE_VERSION = 1;
static final String tableName="Signup";
static final String DATABASE_CREATE = "create table "
+ " Signup "+ " " + " "
+ " ( "
+ " ID "
+ " integer primary key autoincrement , "
+ " NAME text , USERNAME text , PASSWORD text , EMPLOYEE_CODE text,"
+ " MOBILE_NUMBER integer ); ";
public SQLiteDatabase db;
private final Context context;
private DataBaseHelper dbHelper;
这是我的 xml 文件,其中包含列表视图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="6dp"
tools:context=".Friends" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</TabHost>
<ListView
android:id="@+id/listView1"
style="@style/WhiteText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="43dp">
</ListView>
</LinearLayout>