【问题标题】:Unfortunately HelloListView has stopped不幸的是 HelloListView 已停止
【发布时间】:2012-01-25 19:31:44
【问题描述】:

我关注了this tutorial,但是当我尝试运行应用程序时,我得到了Unfortunately HelloListView has stopped IDE 不会给出警告或错误。

我的 HelloViewListActivity.java 看起来像这样:

public class HelloListViewActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] countries = getResources().getStringArray(R.array.countries_array);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, countries));

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // When clicked, show a toast with the TextView text
                Toast.makeText(getApplicationContext(),
                        ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

我的strings.xml 和list_item.xml 都是identical to the tutorial.

Logcat log here 我到底做错了什么?

【问题讨论】:

  • 您的 list_content_simple.xml 似乎不正确。再验证一次。
  • @thinksteep list_content_simple.xml 没有在教程中提到。我确实找到了 SDK 文件夹并将其添加到我的 res/layout 但仍然是同样的错误。
  • 您所指的教程有点零散。现在看看我的回答。我已经包含了工作示例教程的链接,如果你不想要,你可以忽略数据库部分。
  • 使用logcat 获取有关崩溃的更多信息。或者在设备上,使用市场上的 Catlog 应用程序。

标签: java android


【解决方案1】:

你必须在super.onCreate(savedInstanceState);之后添加setContentView(R.layout.yourlayout)

R.layout.yourlayout 应如下所示(在文件夹 res/layout 中):

<?xml version="1.0" encoding="utf-8"?>
<ListView 
  android:id="@android:id/list"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" />

【讨论】:

  • 谢谢。在 LinearLayout 中添加 ListView 解决了我的问题。奇怪的是,在我遵循的 2 个教程中,没有一个提到这一步。
【解决方案2】:

当你扩展 ListActivity 时,你需要有一个包含 id 为@android:id/list 的xml,然后在你的活动中你需要setContentView(R.layout.yourxmlName);这里是tutorial。

【讨论】:

  • 当我有一个 ListActivity 时,我必须总是有 '@android:id/list' 的东西吗?我不能把 ID 改成 '@android:id/list2' 之类的吗?
  • 不,如果你想使用自定义列表(或自定义 id),你必须使用Activity 而不是ListActivity,然后使用findViewById(R.id.thelistid) 获取列表
猜你喜欢
  • 2016-02-29
  • 2017-05-03
  • 2016-10-20
  • 2014-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多