填充列表视图时出现 android 异常

发布于 2024-10-21 21:43:43 字数 1132 浏览 5 评论 0原文

在尝试根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绮筵 2024-10-28 21:43:43

尝试android.R.layout.simple_list_item_1

答案在 simple_list_item_2.xml

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
    ...

您需要使用仅包含 TextView 的资源。 simple_list_item_1.xml 符合要求:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    ...

尝试浏览其他可能性 这里或者自己制作,如果你没有看到你喜欢的。

Try android.R.layout.simple_list_item_1.

The answer is in simple_list_item_2.xml:

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
    ...

You need to use a resource that contains only a TextView. simple_list_item_1.xml fits the bill:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    ...

Try browsing other possibilities here or making your own if you don't see any you like.

战皆罪 2024-10-28 21:43:43

使用新的 ListView 和适配器启动新的 Activity 可能会更好。

您可以将所选项目作为 Intent 中的参数传递:

Intent = new Intent(MyActivity.this, ActivityToStart.class);
intent.putExtra("selected", position);
startActivity(intent);

并稍后在其他 Activity 的 onCreate 方法中检索它:

int position = getIntent().getIntExtra("selected");

您可能需要考虑您想要哪些信息在活动之间共享。使用多个活动将允许您的用户按后退按钮并返回到原始列表。

It would probably be better to start a new Activity with a new ListView and adapter.

You can pass the selected item as a parameter in your Intent:

Intent = new Intent(MyActivity.this, ActivityToStart.class);
intent.putExtra("selected", position);
startActivity(intent);

and retrieve it later in your other activity's onCreate method with:

int position = getIntent().getIntExtra("selected");

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文