2013-03-12 3 views
43

Ich habe unter Code verwendet und es ist nicht grafische Layout zu rendern. Anzeigefehler wie Exception raised during rendering: No tab known for tag null.Android FragmentTabHost: Kein Tab für Tag Null bekannt

wie kann ich das lösen?

<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:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

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

     <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> 
+0

Welchen Code verwenden Sie ? Verwenden Sie ein Menü-XML? Hast du einen Logcat? –

+1

Können Sie den Code, in dem Sie den TabHost initialisieren, posten? –

+3

Ich habe einen Fehlerbericht geöffnet, bitte abstimmen: https://code.google.com/p/android/issues/detail?id=78772 – Gavriel

Antwort

8

Dies ist der Code, den ich verwendet habe, die TabHost und es funktioniert gut zu initialisieren:

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 
      } 
     } 
    } 
} 
+0

Vielen Dank !!! – gc986

6

Es scheint, es ist ein Fehler in Android-Support-Bibliothek.

fand ich endlich diese Abhilfe:

geänderte Layout-Datei als diese:

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

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

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

</LinearLayout> 

dann von Code FragmentTabHost wie dieses Beispiel erstellt i:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    tabHost = new FragmentTabHost(getActivity()); 
    inflater.inflate(R.layout.logs_view, tabHost); 
    tabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent); 

    tabHost.addTab(tabHost.newTabSpec("simple").setIndicator("Simple"), ActivityLogFragment.class, null); 
    tabHost.addTab(tabHost.newTabSpec("contacts").setIndicator("Contacts"), MiniStatementFragment.class, null); 
    return tabHost; 
} 
+1

Keine Änderung ........ – evya

5

ich dieses Problem begegnet, recherchiert, konnte aber keine funktionierende Lösung finden. Aber ich stelle fest, dass dieses Problem nicht auftritt, wenn ich direkt nach "FragmentTabHost # setup" eine Registerkarte hinzufüge, aber wenn ich beispielsweise eine Registerkarte in "AsyncTask # onPostExecute" hinzufüge, tritt dieses Problem auf. Das scheint doof, aber ich habe versucht, es ist .. Basierend auf diesen, fand ich eine Lösung:

Hinzufügen einer leeren Registerkarte direkt nach 'FragmentTabHost # Setup', und dann Hinzufügen von anderen Tabs überall, habe ich versucht, diese Lösung, es funktioniert! Das werde ich das Layout und die Teil-Codes unter:

<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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <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"/> 

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

</LinearLayout> 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); 
    TabHost.TabSpec tabSpec = mTabHost.newTabSpec("TAB_FIRST").setIndicator("First"); 

    mTabHost.addTab(tabSpec, FirstFragment.class, null); 

    TabWidget tabWidget = mTabHost.getTabWidget(); 
    if (tabWidget != null) { 
     View tabWidgetChild = tabWidget.getChildAt(0); 
     if (tabWidgetChild != null) { 
      tabWidgetChild.setVisibility(View.GONE); 
     } 
    } 
} 
0

meine Umgebung entwickeln Android Studio 0.8.9 mit API19 SDK ist.

Wenn ich das FragmentTabHost in eine FragmentActivity setze, funktioniert es. Wenn ich das FragmentTabHost in ein Fragment einfüge, erhält es beim Rendern "keinen Tab, der für Tag null bekannt ist" und erhält Laufzeitfehler, wenn LayoutInflater das Layout aufbläst.

Danke für user3216049's Antwort, es ist eine gute Problemumgehung. Sorry, ich kann nicht wählen, da ich ein Neuling bin. :(

aber es ist nichts in meinem Test Tab Fragmente anzuzeigen. habe ich eine kleine Änderung.

  • test_fragment.xml

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

<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"/> 

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

</LinearLayout> 

  • fragment_section_dummy.
  • xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/text1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center" 
android:textSize="24sp" 
android:padding="32dp" /> 

  • Java-Code Der Punkt ist, dass ich die ID der "R.id.realtabcontent" in FragmentTabHost.setup ändern()

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTabHost; 

public class TestFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, 
       ViewGroup container, Bundle savedInstanceState) { 

     FragmentTabHost tabHost = new FragmentTabHost(getActivity()); 
     inflater.inflate(R.layout.test_fragment, tabHost); 
     tabHost.setup(getActivity(), 
         getChildFragmentManager(), R.id.realtabcontent); 

     tabHost.addTab(tabHost.newTabSpec("simple") 
      .setIndicator("Simple"), DummySectionFragment.class, null); 
     tabHost.addTab(tabHost.newTabSpec("contacts") 
      .setIndicator("Contacts"), DummySectionFragment.class, null); 
     return tabHost; 
    } 

    /** 
    * A dummy fragment representing a section of the app, 
    * but that simply displays dummy text. 
    */ 
    public static class DummySectionFragment extends Fragment { 
     private static int count = 0; 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_section_dummy, 
        container, false); 
      ((TextView) rootView.findViewById(android.R.id.text1)) 
        .setText("Dummy Section " + count++); 
      return rootView; 
     } 
    } 
} 

+0

Wenn Sie eine neue Frage haben, fragen Sie sie bitte, indem Sie auf die Schaltfläche [Frage stellen] (http://stackoverflow.com/questions/ask) klicken. Fügen Sie einen Link zu dieser Frage hinzu, wenn es hilft, Kontext bereitzustellen. – talonmies

+0

Meinst du, dass ich eine neue Frage erstelle und meine Antwort dort ablege? Dann hier einen Link zu meiner neuen Frage einfügen? Eigentlich ist mein Problem dasselbe wie dieser ursprüngliche Beitrag. Ich habe nur eine der Antworten geändert und korrigiert, um das richtige Ergebnis zu erhalten. Vielen Dank. – Jones

1

Dieses Problem tritt auf, wenn Sie versuchen, Ihre FragmentTabHost in OnViewCreated einzurichten, die zu spät ist. Versuchen Sie, es in OnCreateView einzurichten, und fügen Sie mindestens eine Registerkarte hinzu, bevor Sie das Ansichtsobjekt zurückgeben.

3

Ich habe ein ähnliches Problem bei der Verwendung von TabHost in Fragmenten, hat viel gesucht und schließlich eine Lösung gefunden.

1) Halten Sie in auf erstellen Code anzeigen

2) Dies ist der Schlüssel -> Während das Layout für Registerkarte Indikatoren Aufblasen verwenden, wie

folgt
View tabIndicator = LayoutInflater.from(getActivity()).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false); 

Wir müssen die Eltern Wert setzen in der inflater als mTabHost.getTabWidget()

Registerkarte Anzeige Einstellen des obigen Code löste das Problem von

  Java : illegal state exception : no tab know for tag null 

Update:

mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost); 

mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent); 

View tabIndicatorToday = LayoutInflater.from(getActivity()).inflate(R.layout.tab_indicator, mTabHost.getTabWidget(), false); 
     ((TextView) tabIndicatorToday.findViewById(R.id.tv_tab_txt)).setText(getResources().getString(R.string.text)); 

mTabHost.addTab(mTabHost.newTabSpec(getResources().getString(R.string.text)).setIndicator(tabIndicatorToday), Fragment.class, null); 
+0

Bitte fügen Sie einen Beispielcode hinzu, wo sollte ich diesen TabIndicator verwenden? –

+0

Aktualisiert meine Antwort bitte überprüfen Sie es –

0

i mein Problem lösen, indem Sie auf diesen Link beziehe

FragmentTabHost - No tab known for tag null

No Tab bekannt für Tag null seine Mittel TabHost noch nicht initialisiert ist becouse Sie in onCreateView Verfahren sind trrying das ist zu spät

Anruf Tabhost in onResume Methode

Verwandte Themen