【问题标题】:Single fragment in xml gives preview error: "Cyclic include, not valid"xml 中的单个片段给出预览错误:“循环包含,无效”
【发布时间】:2013-10-07 19:40:12
【问题描述】:

当我尝试在 Eclipse 的图形布局中显示一个 android xml 文件时,我收到这条消息:从片段布局上下文菜单中选择预览布局。我按照说明右键单击布局-> 片段布局-> 选择布局。但是,当我尝试选择我的布局(由于某种原因无法显示的文件)时,我收到此错误:“循环包含,无效”。我可以选择所有其他布局,例如 androids 预定义的 list_content。

我做错了什么?

这是我的 xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/example_id"
    android:name="com.example.test.MainFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

这是我的 java:

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

这是一个打印屏幕:

【问题讨论】:

    标签: android eclipse graphical-layout-editor


    【解决方案1】:

    您应该在片段 XML 项中包含一个名称属性。例如:

    android:name="com.example.news.ArticleListFragment"
    

    ...其中“ArticleListFragment”是您要使用的 Fragment 类。完整的 sn-p 是:

    <fragment android:name="com.example.news.ArticleListFragment"
                android:id="@+id/list"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent" />
    

    另请参阅:Fragments 和 Creating a Fragment。

    【讨论】:

    • 是的。然而它并不能解决问题。还有其他建议吗?我用 name 属性更新了问题中的代码。
    • 您的文件的全部内容是什么?它是否具有标准标题:... 此外,它是否在布局容器中?
    • 似乎有一个循环引用需要清除。另外,引用这个XML布局文件的类不应该是“name”属性中给出的那个。
    • 同时提供 MainFragment 的布局文件。MainFragment 的布局包括什么...
    • 真的!在您向我指出正确的方向后,我了解到我正在尝试将片段的预览布局设置为包含片段的视图,这当然会导致循环包含错误。
    【解决方案2】:

    @MiStr 在 cmets 中为他的回答指出了正确的方向后,我明白我得到循环包含错误的原因非常明显。我试图将片段的预览布局设置为包含片段的视图。预览布局应该是片段的根布局,而不是片段本身……根布局是片段onCreateView()方法返回的布局。更多信息在这里http://developer.android.com/guide/components/fragments.html#Creating

    ListFragment 会自动创建一个根布局,这样您就不必重写 onCreateView() 方法。 Eclipse(我不知道 android studio)应该自动预览 ListFragment 的示例列表。

    【讨论】:

      【解决方案3】:

      只导入android.app.Fragment;可以预览! 导入android.support.v4.app.Fragment;无法预览!

      【讨论】:

        猜你喜欢
        • 2019-03-14
        • 1970-01-01
        • 1970-01-01
        • 2015-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-13
        • 1970-01-01
        相关资源
        最近更新 更多