填充列表视图时出现 android 异常
在尝试根据 switch 语句的结果填充 ListView 中的项目时,我遇到了某种错误。应用程序强制在模拟器内关闭,当我通过 Eclipse 调试器运行它时,它显示主线程由于 IllegalStateException 已挂起。
除了它明显表明它已进入某种错误状态之外,我该如何修复它?我试图从 OnItemClickListener 内部完成所有这些操作,以便当单击该项目时, switch 语句会评估单击了哪个项目,然后根据切换的结果将相应的 ListAdapter 分配给 ListView。这是正确的做法吗?如果是这样,下面的代码中的什么内容引发了错误?
final ListView lv = (ListView) findViewById(R.id.main_list);
final String[] autos = getResources().getStringArray(R.array.auto_array);
final ListAdapter la_auto = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, autos);
然后在处理 onclicklistener
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView parent, View v, int position, long id)
{
switch(gallery.getSelectedItemPosition())
{
case 0:
lv.setAdapter(la_auto);
break;
编辑的部分进一步向下:LogCat 堆栈跟踪在此错误处停止,“您必须为 TextView 提供资源 ID,并且堆栈此时挂起:ArrayAdapter.createViewFromResource(int, View) , ViewGroup, int) line: 347
有什么建议吗?我想它与我传递给 onItemClicked 方法的参数有关。
While attempting to populate the items in a ListView following the outcome of a switch statement, I am encountering some sort of error. The application force closes inside the emulator, and when I ran it through the Eclipse debugger it shows the main thread has hung due to an IllegalStateException.
Aside from it meaning the obvious that it has entered some sort of wrong state, how do I fix it? I'm trying to do all of this from inside an OnItemClickListener, so that when the item is clicked, a switch statement evaluates which item was clicked, and then assigns an according ListAdapter to the ListView depending on the outcome of the switch. Is this the correct way to go about it? And if so, what in my code below is throwing the error?
final ListView lv = (ListView) findViewById(R.id.main_list);
final String[] autos = getResources().getStringArray(R.array.auto_array);
final ListAdapter la_auto = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, autos);
And then further down in the portion dealing with the onclicklistener
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView parent, View v, int position, long id)
{
switch(gallery.getSelectedItemPosition())
{
case 0:
lv.setAdapter(la_auto);
break;
EDIT: The LogCat stack trace is stopping at this error, "You must supply a Resource ID for a TextView, and the stack is hung at this point: ArrayAdapter.createViewFromResource(int, View, ViewGroup, int) line: 347
Any suggestions? I imagine it has something to do with the parameters I'm passing to the onItemClicked method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
android.R.layout.simple_list_item_1
。答案在 simple_list_item_2.xml:
您需要使用仅包含
TextView
的资源。 simple_list_item_1.xml 符合要求:尝试浏览其他可能性 这里或者自己制作,如果你没有看到你喜欢的。
Try
android.R.layout.simple_list_item_1
.The answer is in simple_list_item_2.xml:
You need to use a resource that contains only a
TextView
. simple_list_item_1.xml fits the bill:Try browsing other possibilities here or making your own if you don't see any you like.
使用新的
ListView
和适配器启动新的Activity
可能会更好。您可以将所选项目作为
Intent
中的参数传递:并稍后在其他 Activity 的
onCreate
方法中检索它:您可能需要考虑您想要哪些信息在活动之间共享。使用多个活动将允许您的用户按后退按钮并返回到原始列表。
It would probably be better to start a new
Activity
with a newListView
and adapter.You can pass the selected item as a parameter in your
Intent
:and retrieve it later in your other activity's
onCreate
method with:You may want to think about what information you would like to share between the activities. Using multiple activities will allow your user to press the back button and return to the original list as well.