从一个屏幕移动到另一个屏幕时,android listview错误

时间:2014-01-13 17:05:17

标签: java android eclipse listview

您好我正在使用Android中的arraylist填充ListView,点击按钮从第一个屏幕移动到第二个屏幕。但是在单击第一个屏幕后会出现以下错误。任何帮助都非常感谢。

second_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listDisplayData"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

SecondActivity.java

package com.example.listexample;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SecondActivity extends Activity {

ListView listDisplayData;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);

        //reference to listview
        ListView listDisplayData = (ListView)findViewById(R.id.listDisplayData);

        final ArrayList<String> arr = new ArrayList<String>();
        arr.add("name");
        arr.add("address");

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.list_content, arr);

        listDisplayData.setAdapter(adapter);
 };
}

错误:

01-13 16:11:37.028: E/AndroidRuntime(1188): FATAL EXCEPTION: main
01-13 16:11:37.028: E/AndroidRuntime(1188): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listexample/com.example.listexample.DisplayData}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.os.Looper.loop(Looper.java:137)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at java.lang.reflect.Method.invokeNative(Native Method)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at java.lang.reflect.Method.invoke(Method.java:511)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at dalvik.system.NativeStart.main(Native Method)
01-13 16:11:37.028: E/AndroidRuntime(1188): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.ListActivity.onContentChanged(ListActivity.java:243)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.Activity.setContentView(Activity.java:1881)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at com.example.listexample.DisplayData.onCreate(DisplayData.java:26)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.Activity.performCreate(Activity.java:5104)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-13 16:11:37.028: E/AndroidRuntime(1188):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-13 16:11:37.028: E/AndroidRuntime(1188):     ... 11 more

如果需要更多细节,请告诉我。

2 个答案:

答案 0 :(得分:1)

Exception告诉您,ListView需要android.R.id.list作为身份证明。如果您要为ListActivityListFragment创建自己的布局,通常会发生这种情况。这两个都要求你有一个ListView的id,这样你就可以利用每个类提供的便利方法。

Exception课程引发的DisplayData不是SecondActivity。我确信您使用的是ListFragmentListActivity

答案 1 :(得分:0)

我认为错误是错误消息中显示的错误。

Your content must have a ListView whose id attribute is 'android.R.id.list'

重命名ListView的ID,如下所示,

<ListView android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

检查this answer以获取更多信息