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

先来一张效果图:

下面是xml文件:

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



    

        
			
            
            

            
            
        
    


下面是主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:



	
    

        

            
            

            
            
        
    


下面是跳转的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));
	}

}

 

你可能感兴趣的:(android)