android上的ArrayAdapter illegalStateException

时间:2014-09-01 16:49:13

标签: android listview android-arrayadapter

我为我的listView使用了一个arrayAdpater,但是我得到了这个异常,我不明白为什么,我插入了layout.xml的id和{的id {1}}当我初始化listView时,我无法解决此问题

arrayAdpater(文件定义为listView)

create_boundary_map.xml

在函数内部我这样做:

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

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="138dp" >
    </ListView>

</LinearLayout>

我的logcat:

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.create_boundary_map, R.id.list_view,
                this.fooStrings());

提前感谢。

2 个答案:

答案 0 :(得分:2)

Adapter采用TextView而不是ListView的布局。

创建一个新的xml,例如 list_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

然后在Activity的onCreate内部使用你拥有的布局(create_boundary_map.xml):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_boundary_map);
    // Use the adapter to populate your list:
    ListView listView = (ListView) findViewById(R.id.list_view);


    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.item, this.fooStrings());

    listView.setAdapter(arrayAdapter);
}

答案 1 :(得分:1)

这是创建适配器的正确方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_boundary_map.xml);
    //create_boundary_map.xml must contain list_view.   
    ListView myListView = (ListView) findViewById(R.id.list_view);
 ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, this.fooStrings());
    myListView.setAdapter(adapter);
...
...
...
}

或者您可以创建自定义行布局来填充值,例如:

row_layout.xml包含名为myTextView

的TextView
<?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">
         <TextView
            android:id="@+id/myTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/> 
</LinearLayout>

使用row_layout.xml

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_boundary_map.xml);
        //create_boundary_map.xml must contain list_view.   
      ListView myListView = (ListView) findViewById(R.id.list_view);
    ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.row_layout,  R.id.myTextView, this.fooStrings());
    myListView.setAdapter(adapter);
    ...
    ...
    ...
    }

在此处的类似问题中查看我的回答:android listview not working