0

我正在尝试获取 HashMaps 的 ArrayList 以尝试获取有效的 ListAdapter。当我运行这个时,我得到一个

java.lang.RuntimeException: Your content must have a ListView whose id attribute is android.R.id.list

错误。这是我所拥有的:

gameList 是我从中获取数据的地方:

gameList.toString() returns: [{turn=1, opponent=UserTwo, streak=4, hintX=1234, player=UserOne, solution=MySolution, hintY=5678}, {turn=0, opponent=UserThree, streak=12, hintX=1344, player=UserOne, solution=SolutionTwo, hintY=5428}]

我的 ListActivity 类:

public class GameListActivity extends ListActivity{

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

    String GAME_ITEM = "game";
    String PLAYER_ITEM = "player";
    String OPPONENT_ITEM = "opponent";
    String SOLUTION_ITEM = "solution";
    String HINTX_ITEM = "hintX";
    String HINTY_ITEM = "hintY";
    String STREAK_ITEM = "streak";
    String TURN_ITEM = "turn";

ArrayList<HashMap<String, String>> gameList = new ArrayList<HashMap<String, String>>();
AppVars gameVars = ((AppVars) getApplicationContext());
gameList = gameVars.getState();

String[] keyList = new String[] { OPPONENT_ITEM, STREAK_ITEM, TURN_ITEM };

ListAdapter adapter = new SimpleAdapter(this, gameList, R.layout.game_list_item, 
                        keyList, 
                        new int[] {R.id.opponent, R.id.streak, R.id.turn});
setListAdapter(adapter);        

}   

XML:

game_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/thumbnail"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:src="@android:drawable/alert_dark_frame" />

    <TextView
        android:id="@+id/opponent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_centerHorizontal="true"
        android:text="Entry"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/streak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/thumbnail"
        android:layout_centerHorizontal="true"
        android:text="Streak"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/turn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/streak"
        android:layout_centerHorizontal="true"
        android:text="Your turn!"
        android:textSize="20sp" />

</RelativeLayout>

游戏列表.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/blackboard" >

    <ListView
        android:id="@+id/gameList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>
4

5 回答 5

1

java.lang.RuntimeException: 你的内容必须有一个 id 属性为 android.R.id.list 的 ListView

所以错误日志告诉你一切

您需要添加到您的XML文件ListView android.R.id.list

<ListView
   android:id="@android:id/list"
   // next attributes
/>

正是当您Activity从 扩展时ListActivity,您应该使用它。

这是来自文档的信息

ListActivity 有一个默认布局,它由屏幕中央的单个全屏列表组成。但是,如果您愿意,您可以通过在 onCreate() 中使用 setContentView() 设置您自己的视图布局来自定义屏幕布局。为此,您自己的视图必须包含一个 ID 为“@android:id/list”的 ListView 对象(如果它在代码中,则为列表)

因此,有关更多信息,请查看ListActivity

于 2012-06-15T18:14:16.723 回答
1

列表的 id 是“@+id/gameList”应该是“ @android:id/list”,因为你正在使用ListActivity...

于 2012-06-15T18:17:27.290 回答
1
  • 请注意,当你extends ListActivity Then your xml file must have listview which id is @android:id/list.

利用@android:id/list

  <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>
于 2012-06-15T18:16:27.760 回答
1

您正在扩展ListActivity,因此您的视图应该具有使用 id "list" 定义的列表

有关工作示例,请参见此链接。

于 2012-06-15T18:16:44.007 回答
0

从您的代码中删除以下行:

setContentView(...)

ListActivityListView为您处理 ,因此您可以访问ListViewusing this。如果您需要更复杂的布局子类Activity

于 2012-06-15T18:34:55.980 回答