纠结了一天,终于发现的问题.先描述一下吧:
用FragmentTabHost + FragmentActivity 实现了微博的底部Tab,layout布局如下:
<?xml version="1.0" encoding="utf-8"?> <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/realtabcontent" 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" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.v4.app.FragmentTabHost> </LinearLayout>
package com.yuzile.yuzile; import java.util.Map; import com.yuzile.yuzile.util.YuziLog; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTabHost; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.TabHost.TabSpec; public class MainActivity extends FragmentActivity { private FragmentTabHost mTabHost; private final static String[] tags = { "intro", "guide", "artworks", "discovery" }; private final static Class[] tagClasses = { IntroFragment.class, GuideFragment.class, ArtworksFragment.class, DiscoveryFragment.class }; private final static String[] tagIndicators = { "简介", "导览", "作品", "发现" }; private final static int[] tagImages = { R.drawable.tab_bar_intro_bg_selector, R.drawable.tab_bar_guide_bg_selector, R.drawable.tab_bar_artworks_bg_selector, R.drawable.tab_bar_discovery_bg_selector }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); initTabHost(); } private void initTabHost() { mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); findViewById(android.R.id.tabs).setBackgroundResource( R.drawable.bottombarbg); for (int i = 0; i < tags.length; i++) { TabSpec tabSpec = mTabHost.newTabSpec(tags[i]); Drawable drawable = getResources().getDrawable( R.drawable.tab_bar_intro_bg_selector); tabSpec.setIndicator(getIndicatorView(i)); mTabHost.addTab(tabSpec, tagClasses[i], null); } } private View getIndicatorView(int i) { LayoutInflater inflater = getLayoutInflater(); ViewGroup views = (ViewGroup) inflater.inflate( R.layout.tab_widget_item, null); ImageView imageView = (ImageView) views .findViewById(R.id.tab_widget_item_image); TextView textView = (TextView) views .findViewById(R.id.tab_widget_item_text); imageView.setBackgroundResource(tagImages[i]); textView.setText(tagIndicators[i]); return views; } }
刚开始一直进行得非常顺利,但是就在今天,我导入了一个Google Map的Library,地图都进行的很顺利,但是在编译的时候提示
Found 2 versions of android-support-v4.jar in the dependency list,
就是有两个版本的android-support-v4.jar , 我一看是因为Google Map的Library也有一个,两个估计是不一样引起提示,强迫症患者就讨厌就是看到提示了,所以我想用其中一个替换另一个,但是以哪个为准呢?既然Google Map是官方的库,肯定没问题,所以用Google Map Library的android-support-v4.jar替换了我当前的项目的了.好了,麻烦就来了:启动App就会崩掉,并且报错:No tab content FrameLayout found for id xxxxxxx . 原来工作得挺好的,后来就崩了,所以我肯定代码是没问题的.
网上有答案说要自定义FragmentTabHost http://stackoverflow.com/questions/13408709/android-tabs-at-the-bottom-with-fragmenttabhost , 但是我并不像这么做,也没尝试,纠结很久之后,知道是android-support-v4.jar某些版本的Bug引起的,但是我不知道是什么版本,什么版本修好了,官方也没有说明,所以一个思路是找到正确的那个版本,在自己的开发环境新建了一个工程,按照原来的工程配置,但还是一样的错误,我也不知道为什么,也许来大姨妈了. 最后在网上找了一些实例代码下载,先正确运行了,然后偷里面的android-support-v4.jar 替换到项目中,果然可以了,罪魁祸首就是Google Map的android-support-v4.jar ,害得我浪费了一天啊.
后来又崩了,不断提示:
Found 2 versions of android-support-v4.jar in the dependency list,
but not all the versions are identical (check is based on SHA-1 only at this time).
All versions of the libraries must be the same at this time.
就是这个错误导致了启动的时候找不到Activity,所以必须解决两个不同版本的android-support-v4.jar冲突的问题,解决方法当然是覆盖了...
参考网址:http://code.google.com/p/android/issues/detail?id=53230