引起:java.lang.RuntimeException

时间:2014-03-07 07:05:18

标签: android listview android-logcat

我正在编写一个应用程序,我正在使用ListView,但每当我运行我的程序时: -

日志说: -

03-07 12:24:34.030: E/AndroidRuntime(20663): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

onCreate()方法: -

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        contactList = new ArrayList<HashMap<String, String>>();

        ListView lv = getListView();

        // Listview on item click listener
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

            }
        });

        // Calling async task to get json
        new GetContacts().execute();
    }

activity_main.xml中: -

  <ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />

1 个答案:

答案 0 :(得分:3)

将ID设置为:

android:id="@android:id/list"

像这样:

  <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />