views:

2117

answers:

2

Dear Android folks,

I am having an issue with using TabHost in a new Intent of type TabActivity which I hope you can point me in the right direction. Funnily it works fine when I try to view it in the original Intent : setContentView(R.layout.main)

I get a "forced closed" and within logcat, I get the following error even though my Tabhost id = "@android:id/tabhost":

02-18 22:23:11.937: ERROR/AndroidRuntime(5944): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'

I have declared the second intent in the Manifest.xml file: XML:

<activity android:name=".NextActivity" android:label="@string/app_name" > 

Within the first activity (MainActivity), I start the second intent (NextActivity), with extras, as follows:

Intent nextActivity = new Intent(MainActivity.this,NextActivity.class); 
Bundle b_next=new Bundle(); 

b_next.putString("s_string", myString); 

nextActivity.putExtras(b_next); 

In my NextActivity.java file, I get the extras and try to display the TabHost View:

public class NextActivity extends TabActivity { 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 

        String myString; 
        Bundle b_initial; 

        b_initial = getIntent().getExtras(); 

        myString = b_initial.getString("s_string"); 

     setContentView(R.layout.main); 

        } 
}

I get the same error with using the TabHost example on the Android Developer site (Hellow View):

Main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
        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="wrap_content" /> 
        <FrameLayout 
            android:id="@android:id/tabcontent" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"> 
            <TextView 
                android:id="@+id/textview1" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                android:text="this is a tab" /> 
            <TextView 
                android:id="@+id/textview2" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                android:text="this is another tab" /> 
            <TextView 
                android:id="@+id/textview3" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                android:text="this is a third tab" /> 
        </FrameLayout> 
    </LinearLayout> 
</TabHost>

Thanks in advance folks...

CLARIFICATION: This is what I really get from LogCat:

java.lang.NullPointerException
at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java 285) at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java 640) at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java 640) at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java 640) at android.view.ViewRoot.handleMessage(ViewRoot.java 1645) at android.os.Handler.dispatchMessage(Handler.java 99) at android.os.Looper.loop(Looper.java 123) at android.app.ActivityThread.main(ActivityThread.java 3948) at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java 521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java 782) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java 540) at dalvik.system.NativeStart.main(Native Method)

A: 

I have previously constructed tabhosts with an id of android:id="@+id/tabhost". Does this work for you?

You could also consider constructing your tab view programmatically:

    TabHost t = getTabHost();
    TabSpec tab = t.newTabSpec(label)
                   .setIndicator(label, icon)
                   .setContent(intent);
    t.addTab(tab);
Jack Patmos
Hello Jack, I will try adding the id you suggested and then use findViewById.However, there was a mistake in my post earlier on. This is what I actually get from LogCat at setContentView in the new intent: java.lang.NullPointerException at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java.....
Faisal___
Jack, the new Id that you suggested did not work either.
Faisal___
Hi Faisal: I'm not sure if this applies to your situation but in some instances TabHost.setup() must be called before adding any tabs. See http://developer.android.com/intl/de/reference/android/widget/TabHost.html#setup%28%29 . It might be worth exploring
Jack Patmos
A: 

I also encountered this problem. I had an XML file that wasn't linked against my code properly in my res/layout. When I fixed the naming, the problem went away.

SapphireSun