Null pointer exception with Resources object, using FragmentTabHost - android

I have not used TabHost before, and following several tutorials to piece something together, but I keep getting a NullPointerException at my line 81 where I have my Resources class variable when using it to get an icon from a drawable using
TabSpec tabSpecVolcano = tabHost
.newTabSpec("Volcano") // Line 81
.setIndicator("", resrc.getDrawable(R.drawable.ic_tab_volcano))
.setContent(intentVolcano);
Line 81 is the "caused by" in my LogCat. This error is causing the emulator to crash without showing anything at startup. Below I will post my MainSelectorActivity.java, its xml, and the xml layout for the tab belonging to line 81, the Volcano layout (and class too). All other tabs have a very similar layout and class as the Volcano one. Plus I will post my LogCat. Let me know if you need to see other files. Thanks very much.
UPDATE: The original error was solved by instantiating the getResources() inside of the onCreate method. See below for the answer with an explanation of why it worked.
MainSelectorActivity.java
package com.azurespot.disastertimer.app;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.widget.TabHost.TabSpec;
public class MainSelectorActivity extends FragmentActivity {
Resources resrc = getResources();
FragmentTabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_selector);
// TabHost setup
tabHost = (android.support.v4.app.FragmentTabHost)findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//------Zombie tab------
// Creates tab and sets zombie image in view
// tabHost.addTab(tabHost.newTabSpec("zombie").setIndicator("Zombie",
// getResources().getDrawable(R.drawable.ic_tab_zombie)),
// ZombieTab.class, null);
// When icon is clicked, zombie image shows
Intent intentZombie = new Intent().setClass(this, ZombieTab.class);
TabSpec tabSpecZombie = tabHost
.newTabSpec("Zombie")
.setIndicator("", resrc.getDrawable(R.drawable.ic_tab_zombie))
.setContent(intentZombie);
//------Nuclear tab------
// Creates tab and sets nuclear image in view
// tabHost.addTab(tabHost.newTabSpec("nuclear").setIndicator("Nuclear",
// getResources().getDrawable(R.drawable.ic_tab_nuclear)),
// NuclearTab.class, null);
// When icon is clicked nuclear image shows
Intent intentNuclear = new Intent().setClass(this, NuclearTab.class);
TabSpec tabSpecNuclear = tabHost
.newTabSpec("Nuclear")
.setIndicator("", resrc.getDrawable(R.drawable.ic_tab_nuclear))
.setContent(intentNuclear);
//------Tsunami tab------
// Creates tab and sets tsunami image in view
// tabHost.addTab(tabHost.newTabSpec("tsunami").setIndicator("Tsunami",
// getResources().getDrawable(R.drawable.ic_tab_tsunami)),
// TsunamiTab.class, null);
// When icon is clicked tsunami image shows
Intent intentTsunami = new Intent().setClass(this, TsunamiTab.class);
TabSpec tabSpecTsunami = tabHost
.newTabSpec("Tsunami")
.setIndicator("", resrc.getDrawable(R.drawable.ic_tab_tsunami))
.setContent(intentTsunami);
//------Godzilla tab------
// Creates tab and sets tsunami image in view
// tabHost.addTab(tabHost.newTabSpec("godzilla").setIndicator("Godzilla",
// getResources().getDrawable(R.drawable.ic_tab_godzilla)),
// GodzillaTab.class, null);
// When icon is clicked godzilla image shows
Intent intentGodzilla = new Intent().setClass(this, GodzillaTab.class);
TabSpec tabSpecGodzilla = tabHost
.newTabSpec("Godzilla")
.setIndicator("", resrc.getDrawable(R.drawable.ic_tab_godzilla))
.setContent(intentGodzilla);
//------Volcano tab------
// Creates tab and sets volcano image in view
// tabHost.addTab(tabHost.newTabSpec("volcano").setIndicator("Volcano",
// getResources().getDrawable(R.drawable.ic_tab_volcano)),
// VolcanoTab.class, null);
// When icon is clicked volcano image shows
Intent intentVolcano = new Intent().setClass(this, VolcanoTab.class);
TabSpec tabSpecVolcano = tabHost
.newTabSpec("Volcano")
.setIndicator("", resrc.getDrawable(R.drawable.ic_tab_volcano))
.setContent(intentVolcano);
// add all tabs
tabHost.addTab(tabSpecZombie);
tabHost.addTab(tabSpecNuclear);
tabHost.addTab(tabSpecTsunami);
tabHost.addTab(tabSpecGodzilla);
tabHost.addTab(tabSpecVolcano);
//set Zombie tab as default (zero based)
tabHost.setCurrentTab(0);
}
}
activity_main_selector.xml
<RelativeLayout 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"
tools:context="com.azurespot.disastertimer.app.MainSelectorActivity">
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="140dp"
android:orientation="horizontal"
android:layout_marginTop="200dp"
android:gravity="center_horizontal">
<NumberPicker
android:id="#+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="20dp" />
<NumberPicker
android:id="#+id/numberPicker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="20dp" />
<NumberPicker
android:id="#+id/numberPicker3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="20dp" />
</LinearLayout>
</RelativeLayout>
VolcanoTab.java
package com.azurespot.disastertimer.app;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by azuremoss on 4/22/14.
*/
public class VolcanoTab extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.volcano_tab, container, false);
return v;
}
}
volcano_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginTop="0dp"
android:id="#+id/imageButton"
android:layout_gravity="center_horizontal"
android:src="#drawable/volcano_image"
android:text="#string/volcano_fragment_string"/>
</LinearLayout>
LogCat
1111-1111/com.azurespot.disastertimer.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.azurespot.disastertimer.app/com.azurespot.disastertimer.app.MainSelectorActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
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.content.ContextWrapper.getResources(ContextWrapper.java:81)
at com.azurespot.disastertimer.app.MainSelectorActivity.<init>(MainSelectorActivity.java:13)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
            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)

Actually your error is line 13 in MainSelectorActivity. Line 81 is where the null pointer exception occurs in ContextWrapper which originates from a call in line 13 in your activity class.
Line 13 is
Resources resrc = getResources();
Why are you getting an error here? Since you are calling getResources as a declaration, this call happens before the onCreate of your activity.
getResources requires a context which in this case is from your activity, however since the context has not been properly initialized yet, you will get a null pointer exception.
So if you still want to keep your global resrc variable, you will need to simply set it in the onCreate method.
Resources resrc;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
resrc = getResources();
...
}

you should call getResources() in activity's onCreate() method.

Related

Error with method, only after adding other unrelated code

Not sure what to think about this problem. This method I created for my tab setup called tabSetUp(), I created a while ago, and call in my onCreate method, and it has worked successfully since then. Now, I suddenly add code in another area of my main activity, and now I am getting an app crash, saying it is caused by my tab setup method. Does not make sense. Am I missing something? The new code I added has no errors, but does have yellow warnings, saying there might be null pointer exceptions (which I don't really understand). Why is my app crashing? Thanks for your help. LogCat is below.
MainSelectorActivity.java
package com.azurespot.disastertimer.app;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.View;
import android.widget.Button;
import android.widget.TabWidget;
import com.azurespot.disastertimer.app.tabs.GodzillaTab;
import com.azurespot.disastertimer.app.tabs.NuclearTab;
import com.azurespot.disastertimer.app.tabs.TsunamiTab;
import com.azurespot.disastertimer.app.tabs.VolcanoTab;
import com.azurespot.disastertimer.app.tabs.ZombieTab;
import com.azurespot.disastertimer.app.themedactivities.GodzillaThemedActivity;
import com.azurespot.disastertimer.app.themedactivities.NuclearThemedActivity;
import com.azurespot.disastertimer.app.themedactivities.TsunamiThemedActivity;
import com.azurespot.disastertimer.app.themedactivities.VolcanoThemedActivity;
import com.azurespot.disastertimer.app.themedactivities.ZombieThemedActivity;
public class MainSelectorActivity extends FragmentActivity {
Resources resrc;
FragmentTabHost tabHost;
private Button btnStart;
public static final String TABTIMER = "Tab_and_Timer";
private String selectedTab;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_selector);
tabSetUp();
btnStart = (Button) findViewById(R.id.btnStart);
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToThemedActivity();
}
});
tabListener();
changeTabIndicators();
tabAndTimerPersist();
}
public void tabSetUp() {
resrc = getResources();
// TabHost setup & functionality
tabHost = (android.support.v4.app.FragmentTabHost)findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//------Zombie tab------
//Creates tab and sets zombie icon in tab
tabHost.addTab(tabHost.newTabSpec("zombie").setIndicator("",
getResources().getDrawable(R.drawable.ic_tab_zombie_selected)),
ZombieTab.class, null);
//------Nuclear tab------
//Creates tab and sets nuclear icon in tab
tabHost.addTab(tabHost.newTabSpec("nuclear").setIndicator("",
getResources().getDrawable(R.drawable.ic_tab_nuclear_selected)),
NuclearTab.class, null);
//------Tsunami tab------
//Creates tab and sets tsunami icon in tab
tabHost.addTab(tabHost.newTabSpec("tsunami").setIndicator("",
getResources().getDrawable(R.drawable.ic_tab_tsunami_selected)),
TsunamiTab.class, null);
//------Godzilla tab------
//Creates tab and sets tsunami icon in tab
tabHost.addTab(tabHost.newTabSpec("godzilla").setIndicator("",
getResources().getDrawable(R.drawable.ic_tab_godzilla_selected)),
GodzillaTab.class, null);
//------Volcano tab------
//Creates tab and sets volcano icon in tab
tabHost.addTab(tabHost.newTabSpec("volcano").setIndicator("",
getResources().getDrawable(R.drawable.ic_tab_volcano_selected)),
VolcanoTab.class, null);
//set Zombie tab as default (zero based)
tabHost.setCurrentTab(0);
}
// Sets up the tabs to return a value, based on which one is currently selected.
// The int values will be used in the Start button's onClick to go to the corresponding activity.
public void tabListener() {
tabHost.setOnTabChangedListener(new FragmentTabHost.OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
selectedTab = tabId;
}
});
}
public void changeTabIndicators() {
tabHost = (android.support.v4.app.FragmentTabHost)findViewById(R.id.tabhost);
TabWidget widget = tabHost.getTabWidget();
for(int i = 0; i < widget.getTabCount(); i++) {
View v = widget.getChildTabViewAt(i);
// // Look for the title view to ensure this is an indicator and not a divider.
// TextView tv = (TextView)v.findViewById(android.R.id.title);
// if(tv == null) {
// continue;
// }
v.setBackgroundResource(R.drawable.tab_selector_color);
}
}
public void goToThemedActivity() {
if (selectedTab.equals("zombie")) {
Intent i = new Intent(MainSelectorActivity.this, ZombieThemedActivity.class);
startActivity(i);
} else if (selectedTab.equals("nuclear")) {
Intent j = new Intent(MainSelectorActivity.this, NuclearThemedActivity.class);
startActivity(j);
} else if (selectedTab.equals("tsunami")) {
Intent k = new Intent(MainSelectorActivity.this, TsunamiThemedActivity.class);
startActivity(k);
} else if (selectedTab.equals("godzilla")) {
Intent l = new Intent(MainSelectorActivity.this, GodzillaThemedActivity.class);
startActivity(l);
} else if (selectedTab.equals("volcano")) {
Intent m = new Intent(MainSelectorActivity.this, VolcanoThemedActivity.class);
startActivity(m);
}
}
#Override
protected void onPause() {
super.onPause();
tabAndTimerPersist();
}
private void tabAndTimerPersist() {
SharedPreferences prefs = getSharedPreferences(TABTIMER, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("zombie", 0);
editor.putInt("nuclear", 0);
editor.putInt("tsunami", 0);
editor.putInt("godzilla", 0);
editor.putInt("volcano", 0);
editor.commit();
}
}
LogCat
1054-1054/com.azurespot.disastertimer.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.azurespot.disastertimer.app/com.azurespot.disastertimer.app.MainSelectorActivity}: 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 com.azurespot.disastertimer.app.MainSelectorActivity.tabSetUp(MainSelectorActivity.java:60)
at com.azurespot.disastertimer.app.MainSelectorActivity.onCreate(MainSelectorActivity.java:38)
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)
activity_main_selector.xml
<RelativeLayout 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="#7d8794"
tools:context="com.azurespot.disastertimer.app.MainSelectorActivity">
<android.support.v4.app.FragmentTabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"
android:measureAllChildren="true"/>
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal"
android:layout_marginTop="180dp"
android:gravity="center_horizontal">
<NumberPicker
android:id="#+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="0dp" />
<NumberPicker
android:id="#+id/numberPicker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="0dp" />
<NumberPicker
android:id="#+id/numberPicker3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:layout_marginTop="0dp" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:gravity="center" >
<Button
android:id="#+id/btnStart"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ed872d"
android:text="#string/btn_start"
android:onClick="saveAndStartTimer"/>
</RelativeLayout>
</RelativeLayout>
Change this from
tabHost = (android.support.v4.app.FragmentTabHost)findViewById(android.R.id.tabhost);
to
tabHost = (FragmentTabHost)findViewById(R.id.tabhost);

TabHost on Android crashes the app

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"
I am adding this as an answer so other people can use it as solution. However I will accept nKn's answer as he was the most helpfull in the direction of solving the initial problem.
So the solution I found is to:
replace public class main extends Activity {
with public class main extends TabActivity {
(TabActivity has a strikethrough line in my editor, saying it's deprecated)
and then, for accessing the tabHost, instead of doing this:
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);...
I did this:
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("clientii").setIndicator(
"TAB clienti").setContent(new Intent(this,ListClientiTab.class)));
So, like this, I can see the activity with the TABS, and the app does not crash.
I hate this kind of problems that can only be solved (quickly) by using deprecated aproaches.

Android - TabHost / TabWidget

I'm new to android and try to port a iOS app.
Unfortunately I have some trouble to get my base setup working.
I'm trying to implement a similiar navigation to this tutorial:
tutorial
It is more or less a simple TabHost containing several tabs
but instead of using
tabHost.addTab(tabHost.newTabSpec("settings").setIndicator("settings").setContent(R.id.tab1));
as in the tutorial and what is working I'd like to Init my tab with a class like this:
tabHost.addTab(tabHost.newTabSpec("settings").setIndicator("settings").setContent(new Intent(this, SettingsActivity.class)));
Unfortunately the app crashes when I click on the 'settings-tab'.
This is my code so far:
MainActivity:
package xxx;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
public class MainActivity extends Activity implements OnTabChangeListener
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTabs();
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void initTabs()
{
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();
tabHost.addTab(tabHost.newTabSpec("Übersicht").setIndicator("Übersicht").setContent(R.id.tab1)); // <- is working fine
tabHost.addTab(tabHost.newTabSpec("Einstellungen").setIndicator("Einstellungen").setContent(new Intent(this, SettingsActivity.class))); <- crash
tabHost.setOnTabChangedListener(this);
tabHost.setCurrentTab(0);
}
#Override
public void onTabChanged(String tabId)
{
// TODO Auto-generated method stub
}
}
activity_main.xml:
<RelativeLayout xmlns:android=
xmlns:tools=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<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/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
SettingsActivity:
package xxx;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class SettingsActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("This is tab 2");
setContentView(tv);
}
}
Error-message from LogCat:
10-15 03:52:22.711: W/dalvikvm(889): threadid=1: thread exiting with
uncaught exception (group=0x41465700) 10-15 03:52:22.851:
E/AndroidRuntime(889): FATAL EXCEPTION: main 10-15 03:52:22.851:
E/AndroidRuntime(889): java.lang.IllegalStateException: Did you forget
to call 'public void setup(LocalActivityManager activityGroup)'? 10-15
03:52:22.851: E/AndroidRuntime(889): at
android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:747)
10-15 03:52:22.851: E/AndroidRuntime(889): at
android.widget.TabHost.setCurrentTab(TabHost.java:413) 10-15
03:52:22.851: E/AndroidRuntime(889): at
android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154) 10-15
03:52:22.851: E/AndroidRuntime(889): at
android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
10-15 03:52:22.851: E/AndroidRuntime(889): at
android.view.View.performClick(View.java:4240) 10-15 03:52:22.851:
E/AndroidRuntime(889): at
android.view.View$PerformClick.run(View.java:17721) 10-15
03:52:22.851: E/AndroidRuntime(889): at
android.os.Handler.handleCallback(Handler.java:730) 10-15
03:52:22.851: E/AndroidRuntime(889): at
android.os.Handler.dispatchMessage(Handler.java:92) 10-15
03:52:22.851: E/AndroidRuntime(889): at
android.os.Looper.loop(Looper.java:137) 10-15 03:52:22.851:
E/AndroidRuntime(889): at
android.app.ActivityThread.main(ActivityThread.java:5103) 10-15
03:52:22.851: E/AndroidRuntime(889): at
java.lang.reflect.Method.invokeNative(Native Method) 10-15
03:52:22.851: E/AndroidRuntime(889): at
java.lang.reflect.Method.invoke(Method.java:525) 10-15 03:52:22.851:
E/AndroidRuntime(889): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-15 03:52:22.851: E/AndroidRuntime(889): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-15
03:52:22.851: E/AndroidRuntime(889): at
dalvik.system.NativeStart.main(Native Method)
I think the problem is, that my MainActivity is not extended from ActivityGroup and/or that I'm not using the LocalActivityManager. The problem is, both are deprecated. What do I have to change to get it work without using deprecated methods and classes?
Sorry for this perhaps simple question but I found nothing via google and I'm new to android programming :).
As you mentioned you should extend from ActivityGroup , but if you don't want a deprecated Class so you can Use Fragments and FragmentManager : Fragments
Try this
tabHost.addTab(tabHost.newTabSpec("Einstellungen").setIndicator("Einstellungen").setContent(R.id.tab2)));
Change your code like this
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_albums)).setContent(intent);
tabHost.addTab(spec);
See Nice Android tutorial example hello-tabwidget
Do something like,
TabHost tabHost = getTabHost();
Intent intentHome = new Intent().setClass(MainScreen.this, Home.class);
TabSpec tabSpecHome = tabHost.newTabSpec("Home").setIndicator("Home").setContent(intentHome);
tabHost.addTab(tabSpecHome);
tabHost.setCurrentTab(0);
Hope this helps.. :)
Remove "this" from tabHost.addTab(tabHost.newTabSpec("Einstellungen").setIndicator("Einstellungen").setContent(new Intent(this, SettingsActivity.class)));
and replace with getApplicationContext();
like this
tabHost.addTab(tabHost.newTabSpec("Einstellungen").setIndicator("Einstellungen").setContent(new Intent(getApplicationContext(), SettingsActivity.class)));

Android app using TabHost with Multiple Maps causing issue

I am using TabHost in my application and I need to show 2 maps in 2 different tabs. I have been able to integrate maps but the problem is when I move from one tab to another the map hangs/stucks and does not respond i.e. maps only works on one of the screen
Below is xml and class for my main layout which has TabHost
<?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"
android:background="#ff0000" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff0000"
>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f00"
android:foregroundGravity="top"
android:layout_above="#android:id/tabs"
/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#fff"
android:tabStripEnabled="false"
>
</TabWidget>
</RelativeLayout>
</TabHost>
package com.cotechnica.alps;
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.view.Menu;
import android.widget.TabHost;
import com.ankitkedia.alps.R;
public class Main extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Map1.class);
spec = tabHost.newTabSpec("home").setIndicator("Map1",
getResources().getDrawable(android.R.drawable.star_on)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Map2.class);
spec = tabHost.newTabSpec("rescue").setIndicator("Map2",
getResources().getDrawable(android.R.drawable.star_big_off)
).setContent(intent);
tabHost.addTab(spec);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I have seen many people are facing same issue but there is no proper solution available for it.
Any help would be appreciated, I can sent the sample project source if required
Regards,
Ankit
This guy has a great solution
http://www.ankitkedia.com/2013/07/07/android-google-map-v2-use-multiple-maps-in-application/

Android: Moving tabs to bottom causes the app to crash

i use this tutorial http://developer.android.com/resources/tutorials/views/hello-tabwidget.html to create tabs which opens new intents. however, the tabs in the tutorials are located on the top, so I followed this solution here https://stackoverflow.com/a/2710404/301584 to move the tabs to bottom (basically i just move the TabWidget to be after FrameLayout in the xml file, modify the neccessary layout_height and add layout_weight, as suggested by the solution on the link). the problem is, my apps will always forced close when I did this. here's the error reported by logcat
01-09 04:30:09.838: ERROR/AndroidRuntime(336): FATAL EXCEPTION: main
01-09 04:30:09.838: ERROR/AndroidRuntime(336): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.geoflex.trymasak/com.geoflex.trymasak.TryMasaktTabActivity}: java.lang.ClassCastException: android.widget.FrameLayout
01-09 04:30:09.838: ERROR/AndroidRuntime(336): Caused by: java.lang.ClassCastException: android.widget.FrameLayout
01-09 04:30:09.838: ERROR/AndroidRuntime(336): at com.geoflex.trymasak.TryMasaktTabActivity.onCreate(TryMasaktTabActivity.java:34)
this is my full code
package com.geoflex.trymasak;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;
public class TryMasaktTabActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Tab1.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("tabOne");
spec.setContent(intent);
spec.setIndicator("Tab One");
tabHost.addTab(spec);
// Squish the tab a little bit horizontally
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 40;
// Bump the text size up
LinearLayout ll = (LinearLayout) tabHost.getChildAt(0);
android.widget.TabWidget tw = (android.widget.TabWidget) ll.getChildAt(0);
RelativeLayout rllf = (RelativeLayout) tw.getChildAt(0);
TextView lf = (TextView) rllf.getChildAt(1);
lf.setTextSize(20);
// Do the same for the other tabs
intent = new Intent().setClass(this, Tab2.class);
spec = tabHost.newTabSpec("tabTwo");
spec.setContent(intent);
spec.setIndicator("Tab Two");
tabHost.addTab(spec);
tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 40;
RelativeLayout rlrf = (RelativeLayout) tw.getChildAt(1);
TextView rf = (TextView) rlrf.getChildAt(1);
rf.setTextSize(20);
tabHost.setCurrentTab(0);
}
}
and my main.xml file
<?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">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TabWidget
android:id="#android:id/tabs"
android:layout_weight="0"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content" />
</LinearLayout>
</TabHost>
and my tab1.java (tab2.java is the same)
package com.geoflex.trymasak;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Tab1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("This is tab 1");
setContentView(tv);
}
}
Try this to set Tabs in the bottom :
<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg_main"
>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#android:id/tabs"
/>
</RelativeLayout>
</TabHost>
And use the following code for your main activity (thats extends the TabActivity):
public class TabTestActivity extends TabActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this,Tab1.class);
spec = tabHost.newTabSpec("projects").setIndicator("Projects",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this,Tab2.class);
spec = tabHost.newTabSpec("news").setIndicator("News",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
Try this
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:paddingBottom="#dimen/tab_space_top"
android:layout_width="fill_parent" android:paddingLeft="#dimen/tab_space_gap"
android:layout_height="fill_parent" android:paddingRight="#dimen/tab_space_gap" >
<LinearLayout android:id="#+id/tab_relative_layout" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#drawable/background">
<FrameLayout android:id="#android:id/tabcontent" android:layout_weight="1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_below="#android:id/tabs"></FrameLayout>
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0"></TabWidget>
</LinearLayout>
RelativeLayout rllf = (RelativeLayout) tw.getChildAt(0);
I think this is supposed to be casted to Framelayout according to your logs.
set in fragment
if(getActivity() != null && isAdded) {
//do your operation
}

Categories

Resources