2013-03-12 11 views
43

Użyłem pod kodem i nie jest to układ graficzny renderowania. wyświetl błąd jako Exception raised during rendering: No tab known for tag null.Android FragmentTabHost: Brak zakładki dla tagu null

jak mogę to rozwiązać?

<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

Jakiego kodu używasz ? Czy używasz menu xml? Czy masz logcat? –

+1

Czy mógłbyś opublikować kod, w którym zainicjowałeś TabHost? –

+3

Otworzyłem raport o błędzie, zagłosuj na niego: https://code.google.com/p/android/issues/detail?id=78772 – Gavriel

Odpowiedz

8

Jest to kod, który użyłem do zainicjowania TabHost i działa prawidłowo:

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

Dziękuję bardzo !!! – gc986

6

Wydaje się, że jest to błąd w android biblioteki wsparcia.

końcu znalazłem tego obejścia:

zmieniony plik układ jak ten:

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

Potem stworzył kodem FragmentTabHost w tym przykładzie:

@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

Bez zmian ........ – evya

5

poznałem ten problem, badane, ale nie udało się znaleźć żadnego działającego rozwiązania. Ale zauważam, że kiedy dodaję kartę zaraz po "FragmentTabHost # setup", ten problem nie występuje, ale na przykład, kiedy dodaję kartę w "AsyncTask # onPostExecute", ten problem występuje. To wydaje się głupie, ale próbowałem, to jest .. Na podstawie tych, znalazłem rozwiązanie:

Dodanie pustej zakładki zaraz po "FragmentTabHost # setup", a następnie dodanie innych kart w dowolnym miejscu, próbowałem tego rozwiązania, to Prace! Będę układ i kody częściowe poniżej:

<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

Moja rozwijać środowisko jest Android Studio 0.8.9 z API19 SDK.

Jeśli umieści się FragmentTabHost w FragmentActivity, zadziała. Po umieszczeniu fragmentu FragmentTabHost w fragmencie, uzyskuje on "brak karty dla znacznika null" podczas renderowania i uzyskuje błąd runtime, gdy LayoutInflater nadyma układ.

Dziękuję za odpowiedź użytkownika3216049, jest to dobre rozwiązanie. Niestety, nie mogę głosować, ponieważ jestem początkującym. :(

Jednak wyświetli nic w moich badań fragmentów tab. zrobiłem małą modyfikację.

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

  • kod Java Chodzi o to, że mogę zmienić identyfikator z "R.id.realtabcontent" w FragmentTabHost.setup()

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

Jeśli masz nowe pytanie, zadaj je, klikając przycisk [Zadaj pytanie] (http://stackoverflow.com/questions/ask). Dodaj link do tego pytania, jeśli pomoże to w zapewnieniu kontekstu. – talonmies

+0

Masz na myśli to, że tworzę nowe pytanie i umieszczam tam moją odpowiedź? Następnie umieść link do mojego nowego pytania? W rzeczywistości mój problem jest taki sam jak ten oryginalny post. Właśnie zmodyfikowałem jedną odpowiedź i naprawiłem ją, aby uzyskać poprawny wynik. Dzięki. – Jones

1

Ten problem występuje podczas próby konfiguracji fragmentu FragmentTabHost w onViewCreated, co jest zbyt późne. Spróbuj ustawić go w onCreateView i dodać co najmniej jedną kartę przed zwróceniem obiektu widoku.

3

Mam podobny problem z używaniem tabHost we fragmentach, dużo szukałem i wreszcie znalazłem rozwiązanie tego problemu.

1) Trzymaj kod w sprawie Utwórz widok

2) To jest klucz -> Podczas napełniania układu wskaźników zakładce użyć następująco

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

Musimy ustawić wartość nadrzędną w inflater jak mTabHost.getTabWidget()

Ustawianie wskaźnika kartę używając powyższy kod rozwiązać kwestię

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

Aktualizacja:

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

Proszę podać przykładowy kod, gdzie powinienem użyć tej zakładkiIndykator –

+0

Zaktualizowałem odpowiedź, proszę sprawdzić –

0

mi rozwiązać mój problem, odnosząc ten link

FragmentTabHost - No tab known for tag null

Nie zakładka znany ze znacznikiem zerowym jego Środki tabhost nie jest jeszcze zainicjowany poniewaz jesteś trrying w metodzie onCreateView który jest za późno

zadzwoń do tabhost w metodzie onResume

Powiązane problemy