Android:在TabWidget下启动差异活动

时间:2010-01-25 06:27:22

标签: android android-emulator android-widget android-manifest

我正在尝试在每个标签下发布活动。 我试过以下代码

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)

我正朝着正确的方向前进吗?

谢谢

3 个答案:

答案 0 :(得分:1)

你可以试试这个,

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));
        }

}

答案 1 :(得分:1)

确保您拥有AndroidManifest.xml中定义的活动

<activity android:name=".Tab1Activity" android:label="@string/ACTIVITY_TAB1ACTIVITY">
        <intent-filter>
            <action android:name="android.intent.action.TAB1ACTIVITY" />
            <category android:name="android.intent.category.TAB1" />
        </intent-filter>

答案 2 :(得分:1)

您在“Why do I get an error while trying to set the content of a tabspec in android?

中找到与解决方案相同的问题