user7401478
user7401478

Reputation: 1376

Having problems with Android Tabhost

I want to use Tabs in my custom Dialog.

Here's what I got:

[project_settings.xml]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#ffffffff"
android:layout_height="450dip">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text"
    android:id="@+id/text_dialog"
    android:layout_below="@+id/a"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="20dp"
    android:textSize="18sp"
    android:textColor="#ff000000"
    android:layout_centerHorizontal="true"
    android:gravity="center_horizontal" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:text="OK"
    android:id="@+id/btn_dialog"
    android:gravity="center_vertical|center_horizontal"
    android:layout_below="@+id/text_dialog"
    android:layout_marginBottom="20dp"
    android:background="@android:color/holo_green_dark"
    android:layout_centerHorizontal="true"
    android:textColor="#ffffffff" />

<ImageView
    android:layout_width="match_parent"
    android:id="@+id/a"
    android:gravity="center"
    android:background="#DA5F6A"
    android:scaleType="fitCenter"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_weight="1"
    android:layout_height="100dp" />

<TabHost
    android:layout_width="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_height="match_parent"
    android:id="@+id/tabhost">

    <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"
            android:layout_marginTop="44dp">

        </TabWidget>

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

            <LinearLayout
                android:id="@+id/general"
                android:text="OK"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            </LinearLayout>

            <LinearLayout
                android:id="@+id/resources"
                android:text="OK"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            </LinearLayout>

            <LinearLayout
                android:id="@+id/variables"
                android:text="OK"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            </LinearLayout>

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

[ProjectSettings.java]

public class ProjectSettingsWindow {
public void showDialog(Activity activity, String msg){
    final Dialog dialog = new Dialog(activity);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCancelable(false);
    dialog.setContentView(R.layout.project_settings);

    // create the TabHost that will contain the Tabs
    TabHost tabHost = (TabHost)dialog.findViewById(R.id.tabhost);


    TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");
    TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab");
    TabHost.TabSpec tab3 = tabHost.newTabSpec("Third tab");

    tab1.setIndicator("Tab1");
    tab1.setContent(new Intent(""));

    tab2.setIndicator("Tab2");
    tab2.setContent(new Intent(""));

    tab3.setIndicator("Tab3");
    tab3.setContent(new Intent(""));

    tabHost.addTab(tab1);
    tabHost.addTab(tab2);
    tabHost.addTab(tab3);


    TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
    text.setText(msg);

    Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
    dialogButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    dialog.show();

}
}

How do you make native, working tabs? Why doesn't it show up with XML only? I'm getting java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TabWidget.addView(android.view.View)' on a null object reference error when doing tabhost.addTab() even though there is such layout with such ID.

Here's how it looks like in XML: enter image description here

Upvotes: 1

Views: 127

Answers (1)

C0L.PAN1C
C0L.PAN1C

Reputation: 12243

You need to setup() tabHost prior to adding the tabs!

Try adding the following lines of code:

    LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false); 
    mLocalActivityManager.dispatchCreate(savedInstanceState); 
    // create the TabHost that will contain the Tabs
    TabHost tabHost = (TabHost)dialog.findViewById(R.id.tabhost);
    tabHost.setup(mLocalActivityManager);

    TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");

Upvotes: 1

Related Questions