Reputation: 105
I am having a problem with my TabHost activity not loading correctly due to an error. Here's my code, xml, and error logs:
public class TabScreen extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_screen);
TabHost t = getTabHost();
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
//For New Photos
TabSpec newSpec = tabHost.newTabSpec("NewPhoto");
newSpec.setIndicator("NewPhoto", getResources().getDrawable(R.drawable.ic_tab_newphoto));
Intent phNewIntent = new Intent(this, NewPhoto.class);
newSpec.setContent(phNewIntent);
//For List of Taken Photos
TabSpec listSpec = tabHost.newTabSpec("PhotoList");
listSpec.setIndicator("PhotoList", getResources().getDrawable(R.drawable.ic_tab_photolist));
Intent phListIntent = new Intent(this, PhotoList.class);
listSpec.setContent(phListIntent);
//For Map
TabSpec mapSpec = tabHost.newTabSpec("PhotoMap");
mapSpec.setIndicator("PhotoMap", getResources().getDrawable(R.drawable.ic_tab_photomap));
Intent phMapIntent = new Intent(this, PhotoMap.class);
mapSpec.setContent(phMapIntent);
tabHost.addTab(newSpec);
tabHost.addTab(listSpec);
tabHost.addTab(mapSpec);
}
}
And my 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"/>
</LinearLayout>
and finally, my error messages:(There was a long list, but the main one I believe to be the problem is what I'm pasting)
12-02 16:00:06.250: E/AndroidRuntime(3757): FATAL EXCEPTION: main
12-02 16:00:06.250: E/AndroidRuntime(3757): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.wcu.snipsnaps/edu.wcu.snipsnaps.TabScreen}: java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.wcu.snipsnaps/edu.wcu.snipsnaps.NewPhoto}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
There are multiple entries on this website about this problem, but after looking at every one, none of the solutions worked. My xml has the proper @android:id/tabhost, and I deleted my R.java file and cleaned the entire project numerous times. If this helps, when I was doing some debugging, the place where the application fell apart was at the tabHost.addTab(newSpec);
. Anyone know what's going on?
EDIT: Full error log:
12-02 17:02:43.296: E/AndroidRuntime(8121): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.wcu.snipsnaps/edu.wcu.snipsnaps.TabScreen}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.os.Looper.loop(Looper.java:130)
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-02 17:02:43.296: E/AndroidRuntime(8121): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 17:02:43.296: E/AndroidRuntime(8121): at java.lang.reflect.Method.invoke(Method.java:507)
12-02 17:02:43.296: E/AndroidRuntime(8121): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
12-02 17:02:43.296: E/AndroidRuntime(8121): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-02 17:02:43.296: E/AndroidRuntime(8121): at dalvik.system.NativeStart.main(Native Method)
12-02 17:02:43.296: E/AndroidRuntime(8121): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.app.TabActivity.onContentChanged(TabActivity.java:105)
12-02 17:02:43.296: E/AndroidRuntime(8121): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:212)
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.app.Activity.setContentView(Activity.java:1657)
12-02 17:02:43.296: E/AndroidRuntime(8121): at edu.wcu.snipsnaps.TabScreen.onCreate(TabScreen.java:16)
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-02 17:02:43.296: E/AndroidRuntime(8121): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-02 17:02:43.296: E/AndroidRuntime(8121): ... 11 more
Upvotes: 0
Views: 95
Reputation: 105
I figured it out - -
I wasn't properly extending TabActivity in all of my classes to be displayed in the tab.
Thanks for the help!!
Upvotes: 0
Reputation: 319
Try this in code
TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
Instead of :
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
In XML:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
Instead of
<?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">
Also May be you need to change all ids in views from android:id="@android:id...." to like android:id="@+id/....." , @android is reserved for androids default elements
Link for help http://www.androidhive.info/2011/08/android-tab-layout-tutorial/
Upvotes: 1