Android Studio No tab content FrameLayout found for id 问题修复
一、问题描述
这两天遇到一个无厘头的问题,使用FragmentTabHost时报错:No tab content FrameLayout found for id xxx……
首先代码肯定没问题,出现这个问题的话 eclipse 和 Android Studio 的解决方案不一样。
出错时xml文件的代码是这样的:
<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:orientation="vertical"
tools:context="com.cachecats.oldbook.activity.HomeActivity" >
<FrameLayout
android:id="@+id/activity_home_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#6000"
/>
<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"
></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
></FrameLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
二、解决方案
网上说出现问题的原因是某些v4包版本bug引起的,找一个其他版本的v4包替换一下就可以,还有人说把
TabWidget删掉。这两种办法应该都可以,但在eclipse和 Android Studio这两个平台里解决方案略微不一样,确切说是最优的解决方案不一样。
说一下我的经验:
1.在eclipse环境下解决
eclipse解决起来比较容易,直接采用第一种方法,找一个其他版本的v4包替换一下,具体哪个版本我也搞不清,我是随便找了个就可以啦。
2.在Android Studio 环境下解决
刚开始我也是想替换掉v4包,但我发现把v4拷贝进去之后会引发新的问题:
Error:Error converting bytecode to dex: Cause: com.android.dex.DexException: Multiple dex files defi
Android Studio是自带v4包的,你再拷贝一个进去就有两个v4包了,会有冲突。
想删除一个,但发现怎么删都删不掉,delete之后一运行项目又自动添加进来啦。网上查了很久都没有找到怎么删除自带的v4包,如果知道的同学可以告诉我。
这个方法行不通只好用第二种方法,把 TabWidget 的这段代码直接删除:
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></TabWidget>
问题解决。
但原理是什么我并不清楚。也许这就是成长吧,刚开始你做了一件事不知道为什么对,但我相信在以后的某一天,你一定会知道原因的。就像小学时老师让背古诗,“锄禾日当午,汗滴禾下土”你并不知道这几句话是什么意思,但在长大后会慢慢的理解……