Android下上TabHost设置及Did you forget to call 'public void setup(LocalActivityManager activityGroup)解决办法

Android上下TabHost设置及Did you forget to call 'public void setup(LocalActivityManager activityGroup)解决方法

先来一张效果图:

Android下上TabHost设置及Did you forget to call 'public void setup(LocalActivityManager activityGroup)解决办法

下面是xml文件:

首先是第一个activity_main.xml,实现tab在下面的效果:

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

    <TabHost
        android:id="@+id/maintabhost"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
			<!-- 此处FrameLayout和TabWidget的位置注意一下,TabWidget在FrameLayout之下,并且FrameLayout要设置一下权重这个属性 android:layout_weight="1" 这样才能实现tab在下面的效果 -->
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:layout_weight="1" >
            </FrameLayout>

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </TabWidget>
        </LinearLayout>
    </TabHost>

</RelativeLayout>


下面是主Activity:

 我这里继承ActivityGroup而不是继承Activity,是因为我在写代码的时候报了如下的异常:  java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'? ,代码里有我解决方法(也就是继承ActivityGroup,再加上一行代码就行),你也可以先继承Activity看看会不会报这个异常,如果报了的话再改成这个。

public class WeiBoActivity extends ActivityGroup {

	private TabHost mTabHost = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.weibo_main);
				
		mTabHost = (TabHost) findViewById(R.id.maintabhost);
		mTabHost.setup();
		
		// 此处是我解决异常加的一行代码,如果继承Activity的话可将此行注释
		mTabHost.setup(this.getLocalActivityManager());
		
		mTabHost.addTab(mTabHost.newTabSpec("t1").setIndicator("首页")
				.setContent(R.id.tab1));
		mTabHost.addTab(mTabHost.newTabSpec("t2").setIndicator("消息")
				.setContent(new Intent(this, MessageActivity.class)));
		mTabHost.addTab(mTabHost.newTabSpec("t3").setIndicator("好友")
				.setContent(R.id.tab3));
		mTabHost.addTab(mTabHost.newTabSpec("t4").setIndicator("广场")
				.setContent(R.id.tab4));

	}

}

下面是跳转界面的weibo_message.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
	<!-- 此处TabHost的id自已定义 -->
    <TabHost
        android:id="@+id/msgstabhost"
        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" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>


下面是跳转的Activity,依然继承ActivityGroup:

public class MessageActivity extends ActivityGroup {

	private TabHost mTabHostMsg;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.weibo_message);

		mTabHostMsg = (TabHost) this.findViewById(R.id.msgstabhost);
		mTabHostMsg.setup();
		
		mTabHostMsg.setup(this.getLocalActivityManager());
		
		mTabHostMsg.addTab(mTabHostMsg.newTabSpec("t5").setIndicator("系统消息")
				.setContent(R.id.msgtab5));
		mTabHostMsg.addTab(mTabHostMsg.newTabSpec("t6").setIndicator("评论")
				.setContent(R.id.msgtab6));
		mTabHostMsg.addTab(mTabHostMsg.newTabSpec("t7").setIndicator("私信")
				.setContent(R.id.msgtab7));
	}

}