FragmentTabhost关于v4兼容包在18+报bug No tab content FrameLayout found for id xxx
来源:互联网 发布:淘宝必买清单怎么加入 编辑:程序博客网 时间:2024/04/29 03:56
FragmentTabhost关于v4兼容包在18+报bug No tab content FrameLayout found for id xxx
大半夜了,这个问题终于解决了,相信很多人都用过FragmentTabHost写过类似于微信啥的那种,如果的你的v4兼容包是在版本18以前的,你的布局写成这样
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@+id/activity_main_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#FFF1F1F1"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0"/> </android.support.v4.app.FragmentTabHost></LinearLayout>
不会有任何问题,但是如果你的v4包版本在18以上(这里我指的不包括18),就会报一个No tab content FrameLayout found for id xxx的错误,这个异常在v4包的FragmentTabHost中,是这个部分:
private void ensureContent() { if (mRealTabContent == null) { mRealTabContent = (FrameLayout)findViewById(mContainerId); if (mRealTabContent == null) { throw new IllegalStateException( "No tab content FrameLayout found for id " + mContainerId); } } }
但是新旧版的代码都有这个方法,Google了一下基本都说替换v4包,我说tm这尼玛也叫解决方法,果断stackoverflow泡了一会,无果,又撸了一会FragmentTabHost的代码,貌似区别在这里
private void initFragmentTabHost(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, new int[] { android.R.attr.inflatedId }, 0, 0); mContainerId = a.getResourceId(0, 0); a.recycle(); super.setOnTabChangedListener(this); } private void ensureHierarchy(Context context) { // If owner hasn't made its own view hierarchy, then as a convenience // we will construct a standard one here. if (findViewById(android.R.id.tabs) == null) { LinearLayout ll = new LinearLayout(context); ll.setOrientation(LinearLayout.VERTICAL); addView(ll, new FrameLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); TabWidget tw = new TabWidget(context); tw.setId(android.R.id.tabs); tw.setOrientation(TabWidget.HORIZONTAL); ll.addView(tw, new LinearLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0));
private void ensureHierarchy(Context context) {在原有代码上抽出来一个方法,但是并没想到什么头绪,我还真用了替换v4包的方法,换到了旧的18版本,结果到了今天添加一个github码的时候发现gg了,人家用的版本22,这个棘手的问题又得面对了;Google的时候无意间看见了一个说FragmentTabHost的bug,但是还不是很明白、大意就是顺序的问题,结果机智了一下、又stackoverflow,还真找到了,
stackoverflow地址问题在布局上面!上码:
<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/activity_main_container" 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> </android.support.v4.app.FragmentTabHost>
我的Java码是这样写的:
package com.wxh.chattest;import android.os.Bundle;import android.support.v4.app.FragmentTabHost;import android.widget.TabHost;import com.wxh.chattest.base.BaseActivity;import com.wxh.chattest.fragment.MemberFragment;import com.wxh.chattest.fragment.MyFragment;import com.wxh.chattest.widget.TabIndicatorView;public class MainActivity extends BaseActivity implements TabHost.OnTabChangeListener { private static final String TAG_MAIN = "main"; private static final String TAG_FRIEND = "friend"; private static final String TAG_LOG = "log"; private static final String TAG_ME = "me"; private FragmentTabHost tabHost; private TabIndicatorView MainIndicators; private TabIndicatorView FriendIndicators; private TabIndicatorView LogIndicators; private TabIndicatorView MeIndicators; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { //初始化tabhost tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); tabHost.setup(this, getSupportFragmentManager(), R.id.activity_main_container); //添加tabspec TabHost.TabSpec spec = tabHost.newTabSpec(TAG_MAIN); MainIndicators = new TabIndicatorView(this); MainIndicators.setTabIcon(R.drawable.tab_icon_chat_normal, R.drawable.tab_icon_chat_focus); MainIndicators.setTabTitle("主页"); spec.setIndicator(MainIndicators); //添加tab tabHost.addTab(spec, MyFragment.class, null); //添加tabspec spec = tabHost.newTabSpec(TAG_FRIEND); FriendIndicators = new TabIndicatorView(this); FriendIndicators.setTabIcon(R.drawable.tab_icon_chat_normal, R.drawable.tab_icon_chat_focus); FriendIndicators.setTabTitle("朋友"); spec.setIndicator(FriendIndicators); //添加tab tabHost.addTab(spec, MyFragment.class, null); //添加tabspec spec = tabHost.newTabSpec(TAG_LOG); LogIndicators = new TabIndicatorView(this); LogIndicators.setTabIcon(R.drawable.tab_icon_chat_normal, R.drawable.tab_icon_chat_focus); LogIndicators.setTabTitle("日志"); spec.setIndicator(LogIndicators); //添加tab tabHost.addTab(spec, MyFragment.class, null); //添加tabspec spec = tabHost.newTabSpec(TAG_ME); MeIndicators = new TabIndicatorView(this); MeIndicators.setTabIcon(R.drawable.tab_icon_chat_normal, R.drawable.tab_icon_chat_focus); MeIndicators.setTabTitle("我的"); spec.setIndicator(MeIndicators); //添加tab tabHost.addTab(spec,MemberFragment.class, null); //去白线 tabHost.getTabWidget().setDividerDrawable(android.R.color.white); //监听 tabHost.setCurrentTabByTag(TAG_MAIN); MainIndicators.setTabSelected(true); tabHost.setOnTabChangedListener(this); } @Override public void onTabChanged(String tag) { MainIndicators.setTabSelected(false); FriendIndicators.setTabSelected(false); LogIndicators.setTabSelected(false); MeIndicators.setTabSelected(false); if (TAG_MAIN.equals(tag)) { MainIndicators.setTabSelected(true); } else if (TAG_FRIEND.equals(tag)) { FriendIndicators.setTabSelected(true); } else if (TAG_LOG.equals(tag)) { LogIndicators.setTabSelected(true); } else if (TAG_ME.equals(tag)) { MeIndicators.setTabSelected(true); } }}
TabIndicatorView是我自定义的一个组合式控件
0 0
- FragmentTabhost关于v4兼容包在18+报bug No tab content FrameLayout found for id xxx
- No tab content FrameLayout found for id xxx
- No tab content FrameLayout found for id xxxxxxx
- No tab content FrameLayout found for id xxxxxxx
- Android Studio No tab content FrameLayout found for id 问题修复
- 老项目将V4包替换成V7包的时候,同步gradle文件报Error:(4) Error retrieving parent for item: No resource found that match
- No content provider found for permission revoke:
- 关于android-support-v4.jar兼容包
- No suitable driver found for jdbc:XXX
- No typehandler found for property XXX
- No outbound converter found for XXX
- SpringMVC 返回json数据报错IllegalArgumentException: No converter found for return value of type:xxx
- Spring Data JPA 报错 :PropertyReferenceException: No property params found for type xxx!
- Android开发,so文件报错 No implementation found for XXX
- Fragment报java.lang.IllegalArgumentException:No view found for id 0x7f070250的错误
- 转:Fragment报java.lang.IllegalArgumentException:No view found for id 0x的错误
- No property sql found for type xxx.xxx.entity.UserdefinedGroup
- 关于android自定义View时报error: No resource identifier found for attribute ‘XXX’ in package ‘
- JSP无法显示上传图片可能原因:图片中包含中文
- cocos2d-x案例开发大全 新版本开发
- Java基础
- 《数学之美》
- Google Chrome浏览器不能以根用户身份运行解决
- FragmentTabhost关于v4兼容包在18+报bug No tab content FrameLayout found for id xxx
- c++之左值
- 03_操作系统
- Linux初级 文件处理命令
- 为string类添加一个赋值运算符函数
- S:SELECT value 表达式
- windows下tomcat日志输出至文件
- Mac hook苹果系统进程监控劫持
- touch事件的多点触控案例和获取touch的属性