public class MainTabhost extends Activity {

private TabHost tabHost = null;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mymaintab);

 tabHost = (TabHost) findViewById(R.id.mytabhost);
 tabHost.setup(); 

 tabHost.addTab(tabHost.newTabSpec("1").setIndicator(createView(R.drawable.collect_channel_card)).setContent(new Intent(this, MainActivity.class))
);                
                                                                                                   
 tabHost.addTab(tabHost.newTabSpec("2").setIndicator(createView(R.drawable.collect_program_card)).setContent(new Intent(this,
 TwoActivity.class)));
                
 tabHost.setCurrentTab(0);
            
 tabHost.setBackgroundResource(R.drawable.collect_beijing);
}
public View createView(int i)
{
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.collect_card, null);
ImageView iv= (ImageView)view.findViewById(R.id.collect_card_img);
    iv.setBackgroundResource(i);
return view;
}}
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 xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mytabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"       
>                         
<TabWidget
  android:id="@android:id/tabs"
  android:layout_width="fill_parent"
  android:layout_height="61dip"
  android:layout_gravity="top"
>
</TabWidget>
    <FrameLayout
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
    >
   
    </FrameLayout>
</LinearLayout>
</TabHost></LinearLayout>

解决方案 »

  1.   

    不要只给java.lang.IllegalStateException,可以看一下log信息吗??error级别的
      

  2.   

    08-11 07:48:17.592: ERROR/AndroidRuntime(6978): FATAL EXCEPTION: main
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978): java.lang.RuntimeException: Unable to start activity ComponentInfo{cn.yt.test/cn.yt.test.MainTabhost}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     at android.os.Handler.dispatchMessage(Handler.java:99)
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     at android.os.Looper.loop(Looper.java:123)
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     at java.lang.reflect.Method.invokeNative(Native Method)
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     at java.lang.reflect.Method.invoke(Method.java:521)
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     at dalvik.system.NativeStart.main(Native Method)
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978): Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:649)
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     at android.widget.TabHost.setCurrentTab(TabHost.java:323)
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     at android.widget.TabHost.addTab(TabHost.java:213)
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     at cn.yt.test.MainTabhost.onCreate(MainTabhost.java:22)
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    08-11 07:48:17.592: ERROR/AndroidRuntime(6978):     ... 11 more
      

  3.   

    at cn.yt.test.MainTabhost.onCreate(MainTabhost.java:22)去这个页的22行,看看代码,还有是不是Androidmanifest,xml没有注册该页??
      

  4.   

    在使用TabHost时有时出现Did you forget to call 'public void setup(LocalActivityManager activityGroup),主要是没有继承ActivityGroup和获得ActivityManager解决方案:主页面继承ActivityGroup 而不是 Activity加入 tabHost.setup(this.getLocalActivityManager());