user834850
user834850

Reputation:

Android: TabHost is not visible at runtime

I've seen two questions like this, but they don't have answers.

One says : you should build TabHost at Runtime.

I believe that is not the answer. Then what is the purpose of XML design if it should be initialized from code.

So, my question is still the same:

This is the XML design of some TabHost

<?xml version="1.0" encoding="utf-8"?>
<TabHost
    android:id="@+id/tabHost"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/tab0"
                android:orientation="vertical"
                android:background="@color/controlDarkDark"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                android:layout_gravity="center"
                android:visibility="visible">
                <Button
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button on Tab0" />
            </LinearLayout>>

            <LinearLayout
                android:id="@+id/tab1"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <Button
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button on Tab1" />
            </LinearLayout>

        </FrameLayout>
    </LinearLayout>
</TabHost>

I am simplified the structure, and still at runtime I get the screen (see the image). And the query of

TabHost tabHost = (TabHost)findViewById(R.id.tabHost);

returns null. enter image description here

Upvotes: 3

Views: 694

Answers (1)

KeLiuyue
KeLiuyue

Reputation: 8237

Try this.

In the root of the xml.

Add the LinearLayout.

Then add in your code.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab_host);

    TabHost mTabHost = (TabHost) findViewById(R.id.tabs);
    mTabHost.setup();
    mTabHost.addTab(mTabHost.newTabSpec("tab0").setIndicator("title1", null).setContent(R.id.button1));
    mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("title2", null).setContent(R.id.button2));

}

Note

If your xml code worked well,you edited the right xml code.

Android Studio is based solely on the rough rendering of the XML you wrote.

The Android Studio preview interface is not exactly equivalent to the actual interface, and the difference comes from the Android Studio's replication bias for actual interface effects.

And different Theme showed differemt view.

But in the activity,you must write the correct code and be showed in it.

Upvotes: 2

Related Questions