【问题标题】:Error when loading new activity from fragment从片段加载新活动时出错
【发布时间】:2013-06-23 10:59:56
【问题描述】:

我有一个我无法解决的错误。我已经做了好几个小时了,我所看到的每一个地方都给了我同样的回应。

所以这里是我调用活动的方法:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Fragment fragment;
    FragmentManager fm = getSupportFragmentManager();
    switch (item.getItemId()) {
        case R.id.action_add:
            actionBar.hide();
            Toast.makeText(this, "Let's add a dream", Toast.LENGTH_SHORT)
                    .show();

            fragment = new addDreamFragment();
            fm.beginTransaction()
                    .replace(R.id.container, fragment)
                    .commit();
            break;

        case R.id.action_view:
        Toast.makeText(this, "Let's see those dreams...", Toast.LENGTH_SHORT)
                .show();
        Intent i = new Intent(MainActivity.this, DreamListActivity.class);
        startActivity(i);
        break;
        case R.id.action_share:
            Toast.makeText(this, "Menu item 2 selected", Toast.LENGTH_SHORT)
                    .show();
            break;

        default:
            break;
    }
    actionBar.show();
    return true;
}

它从 FragmentActivity 调用到 ListActivity。

这是我继续收到的错误:

06-23 09:02:54.033: ERROR/AndroidRuntime(2035): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{swin.examples.cloud/swin.examples.cloud.DreamListActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

我有三个 xml 文件,一个用于 main,一个用于 listactivity,另一个用于生成列表适配器。有人可以帮我吗?另外,我也想从片段内部开始 listactivity,也将不胜感激在完成这项工作方面的一些帮助!

XML 文件一:片段活动的主要布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/container" />

XML 文件二:ListActivity 的布局: 编辑很奇怪,没有表现出来。这只是一个 FrameLayout 声明。

XML 文件三:列表视图

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5sp"
    android:textSize="25sp" >

<!--Creates the row for the ListAdapter in the ListActivity. -->

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

    <TextView
        android:id="@+id/android:empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no_dreams" />

</TextView>

【问题讨论】:

  • as in log :Your content must have a ListView whose id attribute is 'android.R.id.list' 表示您需要在 xml 中添加 listview id 为 android:id="@android:id/list"
  • 哪个xml?我在生成 android:id/list 的 listrow xml 中有它,它应该在我的 listactivity 的主布局中吗?
  • 在您添加 ListView 的 xml 文件中
  • 在你的xml中试试这个&lt;ListView android:id="@android:id/list"
  • 已添加 XML 文件。加入我的困惑!

标签: android android-intent android-fragments android-fragmentactivity listadapter


【解决方案1】:

我发现了错误。我需要重新排列 xml 文件中的对象。现在一切都解决了。感谢您的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多