I am trying to use TabHost for the first time and I have some issues. Any idea why this does not work?
activity_dashboard.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context="ro.softwarex.bellaapp.testtabhost.app.DashboardActivity">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<TextView android:id="@+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:textColor="#33b5e5"
android:textStyle="bold"
android:textSize="50sp"
android:gravity="center"
android:text="@string/dummy_content" />
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout android:id="@+id/fullscreen_content_controls"
style="?metaButtonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="@color/black_overlay"
android:orientation="horizontal"
tools:ignore="UselessParent">
<Button android:id="@+id/dummy_button"
style="?metaButtonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/dummy_button" />
</LinearLayout>
</FrameLayout>
</FrameLayout>
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context="ro.softwarex.bellaapp.testtabhost.app.main">
<TabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabHost"
android:layout_gravity="center_horizontal|top">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</FrameLayout>
and the java...: DashboardActivity.java
package ro.softwarex.bellaapp.testtabhost.app;
import ro.softwarex.bellaapp.testtabhost.app.util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class DashboardActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
final Button blogin = (Button) findViewById(R.id.dummy_button);
blogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(DashboardActivity.this, main.class);
startActivity(i);
Toast mytoast = Toast.makeText(getApplicationContext(), "Wow it works", Toast.LENGTH_SHORT);
mytoast.show();
}
});
}
}
and main.java
package ro.softwarex.bellaapp.testtabhost.app;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.widget.TabHost;
public class main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
}
}
}
When I click the button in the first activity, I should see the second activity with the tabHost, but all I get is a message saying "Unfortunately, testtabapp stopped working"
I did not continue with the setting up of the tabhost because it gives the same message, so I stripped the code up to the point where it stops working
What am I doing wrong?
(PS.: I am using Android Studio) And I tried to replace the id of the TabHost but the same result comes up, even in LogCat. I do not understand the problem. Cand you test it on your environment and see if you get the same result?
After replacing the id inside the layout for the TabHost, the app does not crash, however the error in LogCat says the same as before. It still complains about tabHost ID but no crashing.
Also, if I want to continue the code by adding the tabs, the application crashes with the same LogCat ?!?!?! That complains about the tabHost id but without stopping the app.
So code added:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
Toast mytoast = Toast.makeText(getApplicationContext(), "It's greater than HONEYCOMB", Toast.LENGTH_SHORT);
mytoast.show();
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
TabHost.TabSpec tab1 = tabHost.newTabSpec("TabClienti");
TabHost.TabSpec tab2 = tabHost.newTabSpec("TabVizite");
TabHost.TabSpec tab3 = tabHost.newTabSpec("TabRaportZi");
TabHost.TabSpec tab4 = tabHost.newTabSpec("TabSync");
// Set the Tab name and Activity
// that will be opened when particular Tab will be selected
tab1.setIndicator("Clientii");
tab1.setContent(new Intent(this,ListClientiTab.class));
tab2.setIndicator("Vizitele");
tab2.setContent(new Intent(this,ListViziteTab.class));
tab3.setIndicator("Raport zi");
tab3.setContent(new Intent(this,RaportZiTab.class));
tab4.setIndicator("Sincronizare");
tab4.setContent(new Intent(this, SyncTab.class));
/** Add the tabs to the TabHost to display. */
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
tabHost.addTab(tab4);
tabHost.setup();
(this was added to the onCreate of the activity that holds the TabHost in it's layout.)
And the LogCat for this code (I removed the errors from the above LogCat so below it only shows what happens when I click the button):
03-05 18:13:44.709 1460-1460/ro.softwarex.bellaapp.testtabhost.app D/AndroidRuntime﹕ Shutting down VM
03-05 18:13:44.709 1460-1460/ro.softwarex.bellaapp.testtabhost.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40a13300)
03-05 18:13:44.749 1460-1460/ro.softwarex.bellaapp.testtabhost.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{ro.softwarex.bellaapp.testtabhost.app/ro.softwarex.bellaapp.testtabhost.app.main}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.widget.TabHost.addTab(TabHost.java:232)
at ro.softwarex.bellaapp.testtabhost.app.main.onCreate(main.java:45)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
03-05 18:13:47.178 1460-1460/ro.softwarex.bellaapp.testtabhost.app I/Process﹕ Sending signal. PID: 1460 SIG: 9
The problem seems to be with the naming of your TabHost
. You named it:
android:id="@+id/tabHost"
But when you create you Activity
file, you're looking for:
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
Which is not the same as you have. Your TabHost
should be named this way:
android:id="@android:id/tabhost"