问题:最近在做仿微信聊天界面,使用FragmentTabHost的时候出现 No tab content FrameLayout found for id 2131492944的错
下面是xml和main_activity
package com.example.scott.myweixin; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTabHost; import android.widget.TabHost.TabSpec; public class MainActivity extends FragmentActivity { private FragmentTabHost tabHost; private final static String TAG_CHAT="chat"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); tabHost.setup(this, getSupportFragmentManager(), R.id.activity_home_container); TabSpec spec=tabHost.newTabSpec(TAG_CHAT); spec.setIndicator("消息"); tabHost.addTab(spec,MyFragment.class,null); } }
<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" > <FrameLayout android:id="@+id/activity_home_container" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" > </FrameLayout> <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" > </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" > </FrameLayout> </android.support.v4.app.FragmentTabHost> </LinearLayout>
也查了一些资料,发现保证错是当mRealTabContent为空时,但是上面的内容并不是空的
private void ensureContent() { if (mRealTabContent == null) { mRealTabContent = (FrameLayout)findViewById(mContainerId); if (mRealTabContent == null) { throw new IllegalStateException( "No tab content FrameLayout found for id " + mContainerId); } }
最后看网上的回答是删掉xml中的Tabwidget 确实删掉就可以运行了.但是不明白是什么原因. 如果有大神看到了,求解答一下
时间: 2024-10-19 04:41:28