40

簡單的android.support.v4.app.FragmentTabHost的圖形佈局在Eclipse或Android Studio中都不會呈現。
控制檯的錯誤,我得到的是一致的:
Exception raised during rendering: No tab known for tag nullFragmentTabHost圖形佈局不會呈現

我使用最基本的XML文件:

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

但發生同樣的錯誤。

我只是想在標籤小部件和框架佈局的上方或下方添加更多視圖。
我不太在意看標籤內容;我只想看到我的佈局 - 的其餘部分,但問題是當android.support.v4.app.FragmentTabHost駐留在佈局中時沒有呈現任何其他視圖。

我已閱讀並試圖從這個問題的答案後解決問題:
Android: Tabs at the bottom with FragmentTabHost
但我不認爲這是我的問題;我不打算把TabWidget放在底部。

我的其他XML文件之一完美打開。發生在Android Studio中

同樣的問題:
Android Studio doesn't render this either

+0

那是錯誤的logcat或控制檯?如果logcat,請發佈。 – anthropomo

+0

這是一個錯誤日誌:在日食: (例外渲染過程中提出的:沒有選項卡標籤空異常詳細已知的窗口登錄>顯示視圖>錯誤日誌) – lory105

+3

FragmentTabHost不會顯示任何內容,該內容將被動態地添加。所以沒有問題他們。當你在代碼中添加標籤並測試它時應該工作。 http://www.androidhive.info/2011/08/android-tab-layout-tutorial/ – mipreamble

回答

0

不知道....但不應該你的佈局有tabwidget的線性佈局上面它tabhost標籤?

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

我開發的應用而回是implented使用tabhost標籤,這是我的佈局是怎麼...一個標籤有一個日曆視圖一個有圖像轉換器和一個有一個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> 

+2

那麼,一個普通的android.widget.TabHost會工作,除了[android.app.TabActivity被棄用] [tabactivity]有利於使用FragmentTabHosts和Fragments。 [tabactivity]:[http://developer.android.com/reference/android/app/TabActivity.html] – EricRobertBrewer

1

你已經有了錯誤不知道(對不起,我現在忙得不可開交所以不能花更多的時間檢查),但在總體上似乎來自支持庫的FragmentTabHost並不關心xml。見我以前回答另一個問題:

FragmentTabHost with horizontal scroll

1

從佈局我得到同樣的Error..so,我通過代碼解決問題只......它的工作fine..Please試試這個代碼

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

如果ü需要把零散的選項卡,在屏幕底部... @fallow下述 -

讓XML文件中像這樣..

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

現在,如果您關心的是在單個分段標籤中打開多個片段......

@follow步驟::

  1. 創建一個容器片段。此容器片段將默認用於您所有的標籤內容。
  2. 對於每一個標籤的內容替換片段ü需要這種容器。

對於回: - 就像你用不同的牀單更換你的牀.. :)

您的容器碎片類,將在不同的標籤不同的使用......「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 --->它的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> 

@如何使用錯那tiner ..

  1. 對於每個片段U需要將被替換爲此容器片段的ID。

@Last您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; 
    } 

} 

希望它可以幫助..... 乾杯!

4

我有同樣的呈現問題以及編譯錯誤。我通過查找,我是不及格片段時,我創建Addtab解決了這一問題。您必須在mTabHost.addTab上傳遞至少一個片段。以下是工作代碼。

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"));