我正在尝试在每个标签下启动活动。
我尝试了以下代码

public class Tab_Proj1 extends TabActivity {

TabHost mTabHost ;

 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

setContentView(R.layout.main);

final Context context = getApplicationContext();

//mTabHost = (TabHost) this.findViewById(R.id.);
mTabHost = getTabHost();
mTabHost.setup();
            mTabHost.addTab(mTabHost.newTabSpec("tab_test4")
    .setIndicator("Contacts")
    .setContent(new Intent().setClass(context, Tab1Activity.class)));


Tab1Activity正在扩展ListActivity,并且出现以下错误:

01-25 11:57:07.352: ERROR/AndroidRuntime(952): Uncaught handler: thread main exiting due to uncaught exception
01-25 11:57:07.382: ERROR/AndroidRuntime(952): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.app.Tab_Proj1/com.android.app.Tab_Proj1.Tab_Proj1}: java.lang.RuntimeException: Could not create tab content because could not find view with id 2131034148
01-25 11:57:07.382: ERROR/AndroidRuntime(952):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
01-25 11:57:07.382: ERROR/AndroidRuntime(952):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
01-25 11:57:07.382: ERROR/AndroidRuntime(952):     at android.app.ActivityThread.access$1800(ActivityThread.java:112)
01-25 11:57:07.382: ERROR/AndroidRuntime(952):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
01-25 11:57:07.382: ERROR/AndroidRuntime(952):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-25 11:57:07.382: ERROR/AndroidRuntime(952):     at android.os.Looper.loop(Looper.java:123)
01-25 11:57:07.382: ERROR/AndroidRuntime(952):     at android.app.ActivityThread.main(ActivityThread.java:3948)
01-25 11:57:07.382: ERROR/AndroidRuntime(952):     at java.lang.reflect.Method.invokeNative(Native Method)
01-25 11:57:07.382: ERROR/AndroidRuntime(952):     at java.lang.reflect.Method.invoke(Method.java:521)
01-25 11:57:07.382: ERROR/AndroidRuntime(952):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
01-25 11:57:07.382: ERROR/AndroidRuntime(952):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
01-25 11:57:07.382: ERROR/AndroidRuntime(952):     at dalvik.system.NativeStart.main(Native Method)
01-25 11:57:07.382: ERROR/AndroidRuntime(952): Caused by: java.lang.RuntimeException: Could not   create tab content because could not find view with id 2131034148
01-25 11:57:07.382: ERROR/AndroidRuntime(952):     at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:539)
01-25 11:57:07.382: ERROR/AndroidRuntime(952):     at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:530)
01-25 11:57:07.382: ERROR/AndroidRuntime(952):     at android.widget.TabHost$TabSpec.setContent(TabHost.java:417)
01-25 11:57:07.382: ERROR/AndroidRuntime(952):     at com.android.app.Tab_Proj1.Tab_Proj1.onCreate(Tab_Proj1.java:52)
01-25 11:57:07.382: ERROR/AndroidRuntime(952):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
01-25 11:57:07.382: ERROR/AndroidRuntime(952):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)


我的方向正确吗?

谢谢

最佳答案

你可以试试看

package MyPACKAGE;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;

public class TabViewExp extends TabActivity {

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            TabHost tabHost = getTabHost();

            LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(), true);
            Intent intent = new Intent(TabViewExp.this, NextActivity.class);
            tabHost.addTab(tabHost.newTabSpec("tab1")
                    .setIndicator("tab1")
                    .setContent(intent));
            tabHost.addTab(tabHost.newTabSpec("tab3")
                    .setIndicator("tab2")
                    .setContent(R.id.view2)
                    );
            tabHost.addTab(tabHost.newTabSpec("tab3")
                    .setIndicator("tab3")
                    .setContent(R.id.view3));
        }

}

07-26 09:42