开发者

Android : launching diff activities under TabWidget

开发者 https://www.devze.com 2022-12-17 20:17 出处:网络
I am trying to launch activities under each tab. I have tried with following code public class Tab_Proj1 extends TabActivity {

I am trying to launch activities under each tab. I have tried with following code

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 is extending ListActivity and I'm getting the following error:

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(ActivityTh开发者_Python百科read.java:2231)

Am I going in correct direction?

Thanks


You may try this,

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

}


Make sure you have the activity defined in the 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>


You find the same problem with a solution in "Why do I get an error while trying to set the content of a tabspec in android?"

0

精彩评论

暂无评论...
验证码 换一张
取 消