views:

1040

answers:

3

hiii I am trying to launch activities under each tab. I hav 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.

I m getting exception as::::::

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.(TabHost.java:539) 01-25 11:57:07.382: ERROR/AndroidRuntime(952): at android.widget.TabHost$ViewIdContentStrategy.(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)

======================================================================

Am I going in correct way? If not please help me with some ideas..... thanks

A: 

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

}
Vinayak.B
What if NextActivity extends ListActivity????
rahul
ListActivity is still an Activity, it works fine inside a TabHost.
mbaird
A: 

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>
A: 

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?"

white_gecko