android开发分享Android的FragmentTabHost:没有标签null的标签

我使用下面的代码,它不是渲染graphics布局。 Exception raised during rendering: No tab known for tag null显示exception作为Exception raised during rendering: No tab known for tag null

我该如何解决这个问题?

 <android.support.v4.app.FragmentTabHost xmlns:android="https://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> 

    这是我用来初始化TabHost的代码,它工作正常:

     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 (eg 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 } } } } 

    看来有一个Android支持库中的错误。

    我终于find了这个解决方法:

    更改布局文件为这一个:

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

    然后我通过代码FragmentTabHost创build这个例子:

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

    我遇到了这个问题,经过研究,却找不到任何工作的解决办法。 但是我注意到,当我在'FragmentTabHost#setup'之后添加一个选项卡时,这个问题不会发生,但是例如,当我在'AsyncTask#onPostExecute'中添加一个选项卡时,会出现此问题。 这似乎是愚蠢的,但我试过了,这是..基于这些,我find了一个解决scheme:

    在'FragmentTabHost#setup'之后添加一个空的标签,然后在其他地方添加其他标签,我尝试了这个解决scheme。 我将在下面的布局和部分代码:

     <android.support.v4.app.FragmentTabHost xmlns:android="https://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); } } } 

    我在使用tabHost碎片时遇到了类似的问题,search了很多,最终find了解决scheme。

    1)让你的代码在创build视图

    2)这是关键 – >在扩展标签指标的布局时使用如下

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

    我们需要将mTabHost.getTabWidget()的父值设置为mTabHost.getTabWidget()

    使用上面的代码设置选项卡指示器解决了问题

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

    更新:

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

    当您尝试在onViewCreated中设置您的FragmentTabHost时发生此问题,这太迟了。 尝试在onCreateView中设置它,并在返回视图对象之前添加至less一个选项卡。

    我的开发环境是带有API19 SDK的Android Studio 0.8.9。

    如果我把FragmentTabHost放在FragmentActivity中,它就可以工作。 当我把FragmentTabHost放到一个片段中时,它在渲染时会得到“没有标签null的标签”,并且在LayoutInflater膨胀布局时得到运行时错误。

    感谢user3216049的回答,这是一个很好的解决方法。 对不起,我不能投票,因为我是一个新手。 ?

    但是,它在我的testing选项卡片段中没有显示。 我做了一个小修改。

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

     <TextView xmlns:android="https://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" /> 

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

    我通过引用这个链接解决了我的问题

    FragmentTabHost – 没有标签null的标签

    没有选项卡已知的标签空它的手段tabhost没有初始化,因为你是在拖延onCreateView方法太迟了

    在onResume方法中调用tabhost

    以上就是android开发分享Android的FragmentTabHost:没有标签null的标签相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/513849.html

      (0)
      上一篇 2020年11月29日
      下一篇 2020年11月29日

      精彩推荐