سؤال

When I try to show an android xml file in the graphical layout in Eclipse I get this message: pick preview layout from the Fragment Layout context menu. I follow the instruction by right clicking the layout --> Fragment Layout --> Choose layout. However, when I try to choose my layout (the file that of some reason can't be displayed) I get this error: "Cyclic include, not valid". I can choose all other layouts such as androids predefined list_content.

What am I doing wrong?

Here is my 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" />

and here is my java:

public class MainActivity extends FragmentActivity {

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

Here is a print screen:

Print screen of "Cyclic include, not valid" error

هل كانت مفيدة؟

المحلول 2

After getting pointed in the right direction by @MiStr in the comments to his answer I understood that the reason I got the cyclic include error were quite obvious. I was trying to set the preview layout of the fragment to a view that contained the fragment. The preview layout should be the root layout of the fragment, not the fragment itself... The root layout is the layout that is returned by the fragments onCreateView() method. More info here http://developer.android.com/guide/components/fragments.html#Creating

ListFragment automatically creates a root layout so that you don't have to override the onCreateView() method. Eclipse (I don't know about android studio) should automatically preview a sample list however for ListFragment.

نصائح أخرى

You should include a name attribute in the fragment XML item.. such as:

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

...where "ArticleListFragment" is the Fragment class that you want to use. Full snippet is:

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

See also: Fragments, and Creating a Fragment.

only import android.app.Fragment; can get the preview! import android.support.v4.app.Fragment; can't get preview!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top