web-dev-qa-db-fra.com

La disposition graphique de FragmentTabHost ne rend pas

La présentation graphique pour un simple Android.support.v4.app.FragmentTabHost ne s'affiche jamais dans Eclipse ni dans Android Studio.
L’erreur de console que j’obtiens est systématiquement:
Exception raised during rendering: No tab known for tag null

J'utilise le fichier XML le plus basique:

<Android.support.v4.app.FragmentTabHost
    xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:id="@Android:id/tabhost"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent">

    <LinearLayout
        Android:orientation="vertical"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent">

        <TabWidget
            Android:id="@Android:id/tabs"
            Android:orientation="horizontal"
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            Android:layout_weight="0"/>

        <FrameLayout
            Android:id="@Android:id/tabcontent"
            Android:layout_width="0dp"
            Android:layout_height="0dp"
            Android:layout_weight="0"/>

        <FrameLayout
            Android:id="@+id/realtabcontent"
            Android:layout_width="match_parent"
            Android:layout_height="0dp"
            Android:layout_weight="1"/>

    </LinearLayout>
</Android.support.v4.app.FragmentTabHost>

mais la même erreur se produit. 

Je voulais simplement ajouter d'autres vues au-dessus ou au-dessous du widget à onglets et de la disposition des cadres.
Je me fiche de voir le contenu des onglets; Je veux juste voir le reste de ma mise en page} _ - mais le problème est que AUCUNE AUTRE VUE n'est rendue lorsqu'unAndroid.support.v4.app.FragmentTabHostréside dans la mise en page

J'ai lu et essayé de résoudre le problème à partir de la réponse à ce message:
Android: Onglets en bas avec FragmentTabHost
mais je ne pense pas que ce soit mon problème; Je ne cherche pas à mettre un TabWidget sur le fond.

Tous les autres de mes fichiers XML s’ouvrent parfaitement. 

Le même problème se produit dans Android Studio:
Android Studio doesn't render this either

40
EricRobertBrewer

J'ai eu le même problème de rendu ainsi qu'une erreur de compilation. J'ai résolu le problème en constatant que je ne transmettais pas Fragment lorsque je créais Addtab. Vous devez transmettre au moins un fragment sur mTabHost.addTab. Ci-dessous le code de travail.

private FragmentTabHost mTabHost;
mTabHost = (FragmentTabHost)findViewById(Android.R.id.tabhost);
            mTabHost.setup(HomeActivity.this, getSupportFragmentManager(), Android.R.id.tabcontent);

            mTabHost.addTab(mTabHost.newTabSpec("home").setIndicator("Home"), HomeFragment.class, null);
            mTabHost.addTab(mTabHost.newTabSpec("mysheets").setIndicator("MySheets"));
            mTabHost.addTab(mTabHost.newTabSpec("bookmarks").setIndicator("Bookmarks"));
4
Nauman Ash

Vous n'êtes pas sûr de l'erreur que vous avez commise (désolé, je suis vraiment occupé, je ne peux donc pas passer plus de temps à vérifier), mais en général, il semble que la variable FragmentTabHost de la bibliothèque de support ne se soucie pas du tout du xml. Voir ma réponse précédente à une autre question:

FragmentTabHost avec défilement horizontal

1
J.Nieminen

De la mise en page, je reçois la même erreur..donc, je résous ce problème uniquement par le code ... ça fonctionne très bien..Veuillez essayer ce code

import Android.os.Bundle;
import Android.support.v4.app.Fragment;
import Android.support.v4.app.FragmentTabHost;
import Android.view.LayoutInflater;
import Android.view.View;
import Android.view.ViewGroup;

public class DetailFragment extends Fragment {

    /******************************************************************************************************************
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
     *****************************************************************************************************************/
    public DetailFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // R.layout.fragment_tabs_pager contains the layout as specified in your question
        View rootView = inflater.inflate(R.layout.fragment_tabs_pager, container, false);

        // Initialise the tab-Host
        FragmentTabHost mTabHost = (FragmentTabHost) rootView.findViewById(R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

        // Initialise this list somewhere with the content that should be displayed
        List<String> itemsToBeDisplayed;

        for (String subItem : itemsToBeDisplayed) {
            // Give along the name - you can use this to hand over an ID for example
            Bundle b = new Bundle();
            b.putString("TAB_ITEM_NAME", subItem);

            // Add a tab to the tabHost
            mTabHost.addTab(mTabHost.newTabSpec(subItem).setIndicator(subItem), YourContentFragment.class, b);
        }
        return rootView;
    }
}



/********************************************************
This class contains the actual content of a single tab  
**********************************************************/
public class YourContentFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle extras = getArguments();
        if (extras != null) {
            if (extras.containsKey("TAB_ITEM_NAME")) {
                String subItem = extras.getString("TAB_ITEM_NAME");
                // Do something with that string
            }
        }
    }
}
1
Arun Kumar

Si vous devez placer des onglets fragmentés en bas de l’écran ...

Faites votre fichier XML comme ça.

    <LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:orientation="vertical" >

       <!--   <RelativeLayout 
            Android:layout_width="fill_parent" 
            Android:layout_height="fill_parent">  Android:layout_alignParentTop="true"  -->

         <FrameLayout
            Android:id="@+id/realtabcontent"
            Android:layout_width="match_parent"
            Android:layout_height="0dip"
            Android:layout_weight="1" />


        <Android.support.v4.app.FragmentTabHost
            Android:id="@Android:id/tabhost"
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            >

            <FrameLayout
                Android:id="@Android:id/tabcontent"
                Android:layout_width="0dip"
                Android:layout_height="0dip"
                Android:layout_weight="0" />

        </Android.support.v4.app.FragmentTabHost>

    </LinearLayout>

Maintenant, si votre préoccupation ouvre plusieurs fragments avec des onglets fragmentés uniques ...

@ suivre les étapes ::

  1. Créez un fragment de conteneur. Ce fragment de conteneur sera la valeur par défaut pour tout le contenu de vos onglets.
  2. Pour chaque contenu d'onglet, remplacez le fragment U nécessaire par ce conteneur. 

Par exemple: - Juste comme vous remplacez votre lit avec des draps différents .. :)

Votre classe de fragment de conteneur qui sera utilisée différemment dans différents onglets ... "LearnContainerFragment.Java"

    public class LearnContainerFragment extends BaseContainerFragment {

        private boolean mIsViewInited;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Log.e("test", "tab 1 oncreateview");
            return inflater.inflate(R.layout.container_fragment, null);
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            Log.e("test", "tab 1 container on activity created");
            if (!mIsViewInited) {
                mIsViewInited = true;
                initView();
            }
        }

        private void initView() {
            Log.e("test", "tab 1 init view");
            replaceFragment(new Learn(), false);
        }

    }

LearnContainerFragment.Java ---> c'est un fichier XML container_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
        Android:id="@+id/container_framelayout"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent">


    </FrameLayout>

@ Comment utiliser Conatiner ..

  1. Pour chaque fragment, les besoins seront remplacés par l'identifiant de ce fragment de conteneur.

@last votre classe BaseContainerFragment.Java -

public class BaseContainerFragment extends Fragment {

    public void replaceFragment(Fragment fragment, boolean addToBackStack) {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        if (addToBackStack) {
            transaction.addToBackStack(null);
        }
        transaction.replace(R.id.container_framelayout, fragment);
        transaction.commit();
        getChildFragmentManager().executePendingTransactions();
    }

    public boolean popFragment() {
        Log.e("test", "pop fragment: " + getChildFragmentManager().getBackStackEntryCount());
        boolean isPop = false;
        if (getChildFragmentManager().getBackStackEntryCount() > 0) {
            isPop = true;
            getChildFragmentManager().popBackStack();
        }
        return isPop;
    }

}

J'espère que ça aide..... À votre santé!

1
AndroidHacker

pas sûr ... mais votre mise en page ne devrait-elle pas contenir une balise tabhost au-dessus de la mise en page linéaire du tabwidget?

<TabHost
    Android:id="@+id/tabhost"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:background="@drawable/background"
>

j'ai créé une application il y a quelque temps qui impliquait des onglets à l'aide de tabhost et voici comment était ma mise en page ... un onglet avait une vue du calendrier, un avait un sélecteur d'image et l'autre un listview ...

<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:background="@drawable/background"
tools:context=".MainActivity" >

<TabHost
    Android:id="@+id/tabhost"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:background="@drawable/background"
>

    <LinearLayout
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:background="@drawable/background"
        Android:orientation="vertical" >

        <TabWidget
            Android:id="@Android:id/tabs"
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content" >

        </TabWidget>

        <FrameLayout
            Android:id="@Android:id/tabcontent"
            Android:layout_width="match_parent"
            Android:layout_height="match_parent" >

            <LinearLayout
                Android:id="@+id/tab1"
                Android:layout_width="match_parent"
                Android:layout_height="match_parent"
                Android:orientation="vertical" >

                <ListView
                    Android:id="@+id/listView1"
                    Android:layout_width="match_parent"
                    Android:layout_height="match_parent" >
                </ListView>
            </LinearLayout>

            <LinearLayout
                Android:id="@+id/tab2"
                Android:layout_width="match_parent"
                Android:layout_height="match_parent"
                Android:orientation="vertical" >

                <ImageSwitcher
                    Android:id="@+id/imageSwitcher1"
                    Android:layout_width="match_parent"
                    Android:layout_height="251dp" >
                </ImageSwitcher>

                <TextView
                    Android:id="@+id/tv"
                    Android:layout_width="match_parent"
                    Android:layout_height="wrap_content"
                    Android:scrollbars="vertical" />

            </LinearLayout>

            <LinearLayout
                Android:id="@+id/tab3"
                Android:layout_width="match_parent"
                Android:layout_height="match_parent"
                Android:orientation="vertical" >

                <CalendarView
                    Android:id="@+id/calendarView1"
                    Android:layout_width="match_parent"
                    Android:layout_height="match_parent" />
            </LinearLayout>

        </FrameLayout>
    </LinearLayout>

</TabHost>

0
Ali Elgazar