简体   繁体   English

Android列表视图:未找到资源异常

[英]Android listview: Resource Not Found Exception

I've got problem with Listview in Android. 我在Android中遇到了Listview的问题。 Then I try to set adapter to a listview I got Resource Not Found Exception. 然后我尝试将适配器设置为listview我得到资源未找到异常。

My code: 我的代码:

public class MyActivity extends Activity {
    ArrayList<String> list;

    public void onCreate(Bundle savedInstanceState) {
        list = new ArrayList<String>();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        list.add("first");

        listView = (ListView)findViewById(R.id.CheckpointList);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.CheckpointList, list);
        listView.setAdapter(adapter);
    }
}

And in my main.xml I've got: 在我的main.xml中,我得到了:

<ListView android:id="@+id/CheckpointList" android:layout_height="wrap_content" android:layout_width="fill_parent"></ListView>

I've tried to clean and refesh project - no effects... 我试图清理和改造项目 - 没有影响......

How to solve this problem? 如何解决这个问题呢?

Cheers. 干杯。

The resource you hand over to the ArrayAdapter should not be the id of the ListView. 您交给ArrayAdapter的资源不应该是ListView的id。 It should be the textViewResourceId - which is basically which TextView-layout-id you want your items, in the list, to be rendered as. 它应该是textViewResourceId - 它基本上是你希望列表中的项目呈现为的TextView-layout-id。

One of the standards is eg android.R.layout.simple_list_item_1 . 其中一个标准是例如android.R.layout.simple_list_item_1

Here's an example of a simple ListView: 这是一个简单的ListView示例:

public class ListviewExample extends Activity
{
    private ListView listView;
    private String listView_data[] = {"Android","iPhone","BlackBerry","AndroidPeople"};

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        listView = (ListView) findViewById(R.id.ListView1);
        // By using setAdapter method in listview we add the string array to the ListView.
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 , listView_data));
    }
}

The ressource you hand over to the ArrayAdapter should not be the id of the listview. 您交给ArrayAdapter的资源不应该是listview的id。 It should be the layout ressource of the textview in the listview. 它应该是listview中textview的布局资源。 Look at the documentation: ArrayAdapter(Context context, int textViewResourceId, List objects) 查看文档: ArrayAdapter(Context context,int textViewResourceId,List objects)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM