0

在 onCreate 中:

TextView tv = new TextView(this);
tv.setId(1);
ListView lView = new ListView(this);
String[] lStr = new String[] { "A", "B", "C" };
ArrayAdapter<String> lAdap = new ArrayAdapter<String>(this, tv.getId(), lStr);
lView.setAdapter(lAdap);
setContentView(lView);

错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dtl/com.example.dtl.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x2
4

3 回答 3

1

似乎您在此处使用公共 ArrayAdapter (Context context, int textViewResourceId, T[] objects)textViewResourceId - 包含 TextView 的布局文件的资源 ID,以在实例化视图时使用。在您的代码中,它不是那个 id。您需要资源 ID,而不是视图 ID。我建议检查一些适配器示例(例如这个

于 2012-07-19T03:51:24.550 回答
0

用这个:

YourList.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, YourArray));
于 2012-07-19T05:09:38.197 回答
0

尝试使用android.R.layout.simple_list_item_1而不是textview

于 2012-07-19T03:45:21.430 回答