-1

您好我在我的活动中创建了一个列表视图,我想在其中显示数据库中的内容。但是发生了一个错误,说没有找到列表视图有人可以给我一个更好的解决方案来克服这个错误吗?

我的日志猫如下

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>
4

5 回答 5

2

这是因为您在 setContentView 之前调用 displayResultList() 函数,毕竟在找到 ListView 的 id 之前......

所以改变它:

 @Override
  protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_friends);


   listview=(ListView)findViewById(R.id.listView1);
    openAndQueryDatabase();
    displayResultList();}
于 2013-08-21T07:19:57.667 回答
2

在您的代码中,您已在方法setContentView内部调用onCreate。您的R.layout.activity_friends文件包含一个列表视图,但它的 id 是listView1.

如果您仔细查看异常,您会发现 android 需要一个 id 为 的列表视图list

您可以执行以下操作之一来解决异常

  1. 将 xml 中 listView 的 id 从更改listView1list

  2. 由于您的活动从 ListActivity 扩展,因此它继承了一个 ID 为 的 ListView android.R.id.list。在这种情况下,只需注释对 setContentView() 方法的调用。

  3. 从 Activity 而不是 ListActivity 扩展。

请阅读 ListActivity 类的 android 文档http://developer.android.com/reference/android/app/ListActivity.html

于 2013-08-21T07:29:57.917 回答
0

因为你的活动扩展了 ListActivity ,ListActivity 包含一个 ListView ,你可以通过代码 this.mListView = getListView(); 获取 ListView,你不需要 setContentView()

于 2013-08-21T07:45:16.653 回答
0

不要扩展列表活动只是活动这个解决

于 2013-08-21T07:18:41.087 回答
0

1、删除“setContentView(R.layout.activity_friends);” 2.mothed "findViewById()" 应该被 mothed getListView() 代替;

于 2013-08-21T07:54:09.153 回答