Android Tab Views - could not create tab content because could not find view with id

jimbob picture jimbob · Apr 17, 2012 · Viewed 8.6k times · Source

I am trying to create a tab view with will switch to a different content view by Id. Here is my tab activity:

public class TabViews extends TabActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabviews);
    TabHost tabHost = getTabHost();

    LayoutInflater.from(this).inflate(R.layout.tabviews, tabHost.getTabContentView(), true);

    tabHost.addTab(tabHost.newTabSpec("apples")
            .setIndicator("Apples")
            .setContent(R.id.apples));
    tabHost.addTab(tabHost.newTabSpec("events")
            .setIndicator("Events")
            .setContent(R.id.events));
    tabHost.addTab(tabHost.newTabSpec("attend")
            .setIndicator("Attending")
            .setContent(R.id.attend));
}
}

Here is the tabviews xml layout.

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView android:id="@+id/view1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="test"/>   

<TextView android:id="@+id/view2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="test"/>

<TextView android:id="@+id/view3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="test"/>

</FrameLayout>

I have three other xml layouts attend, events and apples which i can access by creating a new intent the calling startActivity on that Intent.

Here is my R.java class:

public final class R {
public static final class attr {
}
public static final class drawable {
    public static final int bdgdefault=0x7f020000;
    public static final int logo=0x7f020001;
    public static final int black_0=0x7f020002;
    public static final int black_1=0x7f020003;
    public static final int black_2=0x7f020004;
    public static final int black_3=0x7f020005;
    public static final int black_4=0x7f020006;
    public static final int blue_0=0x7f020007;
    public static final int blue_1=0x7f020008;
    public static final int blue_2=0x7f020009;
    public static final int blue_3=0x7f02000a;
    public static final int blue_4=0x7f02000b;
    public static final int brown_0=0x7f02000c;
    public static final int brown_1=0x7f02000d;
    public static final int brown_2=0x7f02000e;
    public static final int brown_3=0x7f02000f;
    public static final int brown_4=0x7f020010;
    public static final int btn_black=0x7f020011;
    public static final int btn_black_1=0x7f020012;
    public static final int btn_black_off=0x7f020013;
    public static final int btn_dropdown_down=0x7f020014;
    public static final int btn_dropdown_left_only=0x7f020015;
    public static final int btn_dropdown_left_right_collapsed=0x7f020016;
    public static final int btn_dropdown_neither=0x7f020017;
    public static final int btn_dropdown_neither_up_down=0x7f020018;
    public static final int btn_dropdown_right_only=0x7f020019;
    public static final int btn_dropdown_up=0x7f02001a;
    public static final int btn_dropdown_up_down=0x7f02001b;
    public static final int btn_white=0x7f02001c;
    public static final int button=0x7f02001d;
    public static final int grac=0x7f02001e;
    public static final int ic_launcher=0x7f02001f;
    public static final int ic_menu_jitsu=0x7f020020;
    public static final int ic_menu_jiu=0x7f020021;
    public static final int ic_menu_name=0x7f020022;
    public static final int mainlogo=0x7f020023;
    public static final int purple_0=0x7f020024;
    public static final int purple_1=0x7f020025;
    public static final int purple_2=0x7f020026;
    public static final int purple_3=0x7f020027;
    public static final int purple_4=0x7f020028;
    public static final int red_0=0x7f020029;
    public static final int red_1=0x7f02002a;
    public static final int mylogo=0x7f02002b;
    public static final int spinner_press=0x7f02002c;
    public static final int spinner_select=0x7f02002d;
    public static final int white_0=0x7f02002e;
    public static final int white_1=0x7f02002f;
    public static final int white_2=0x7f020030;
    public static final int white_3=0x7f020031;
    public static final int white_4=0x7f020032;
}
public static final class id {
    public static final int amattendees=0x7f060006;
    public static final int attend=0x7f060013;
    public static final int attendam=0x7f060000;
    public static final int attendboth=0x7f060002;
    public static final int attendpm=0x7f060001;
    public static final int back=0x7f06000f;
    public static final int bdg=0x7f06000b;
    public static final int bdgBackground=0x7f060009;
    public static final int btn_login=0x7f06001b;
    public static final int btn_newuser=0x7f06001c;
    public static final int description=0x7f060011;
    public static final int details=0x7f06000c;
    public static final int emailaddress=0x7f060019;
    public static final int events=0x7f06000e;
    public static final int grouplogo=0x7f060012;
    public static final int mainlogo=0x7f060018;
    public static final int nearestvenue=0x7f060016;
    public static final int notattending=0x7f060003;
    public static final int password=0x7f06001a;
    public static final int playbutton=0x7f060014;
    public static final int pmattendees=0x7f060007;
    public static final int progressBar=0x7f06000d;
    public static final int qrdecode=0x7f060015;
    public static final int rewarddate=0x7f060010;
    public static final int textView1=0x7f060017;
    public static final int title=0x7f060008;
    public static final int tv_am=0x7f060004;
    public static final int tv_pm=0x7f060005;
    public static final int view1=0x7f06001d;
    public static final int view2=0x7f06001e;
    public static final int view3=0x7f06001f;
    public static final int wv_qr=0x7f06000a;
}
public static final class layout {
    public static final int attend=0x7f030000;
    public static final int apples=0x7f030001;
    public static final int events=0x7f030002;
    public static final int list_items_attendees=0x7f030003;
    public static final int main=0x7f030004;
    public static final int tabviews=0x7f030005;
}
public static final class string {
    public static final int AM=0x7f040009;
    public static final int Login=0x7f040002;
    public static final int Update=0x7f04000a;
    public static final int am=0x7f04000d;
    public static final int amattendees=0x7f040011;
    public static final int app_name=0x7f040001;
    public static final int attend_both=0x7f04000f;
    public static final int attend_pm=0x7f04000e;
    public static final int back=0x7f040005;
    public static final int badge_details=0x7f040006;
    public static final int email_text=0x7f040007;
    public static final int empty=0x7f04000c;
    public static final int events=0x7f040004;
    public static final int hello=0x7f040000;
    public static final int new_user=0x7f040003;
    public static final int not_attending=0x7f040010;
    public static final int password_text=0x7f040008;
    public static final int pmattendees=0x7f040012;
    public static final int qrdecode=0x7f04000b;
}
public static final class style {
    public static final int ButtonText=0x7f050000;
}
}

Here is the error I am receiving:

04-17 10:14:48.608: E/AndroidRuntime(11307):java.lang.RuntimeException: Unable to start activity ComponentInfo{org.jujitsu.app.com/org.jujitsu.app.com.TabViews}:java.lang.RuntimeException: Could not create tab content because could not find view with id 2131099659

more:

04-17 10:14:48.608: E/AndroidRuntime(11307):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
04-17 10:14:48.608: E/AndroidRuntime(11307):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
04-17 10:14:48.608: E/AndroidRuntime(11307):    at android.app.ActivityThread.access$1500(ActivityThread.java:132) 
04-17 10:14:48.608: E/AndroidRuntime(11307):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
04-17 10:14:48.608: E/AndroidRuntime(11307):    at android.os.Handler.dispatchMessage(Handler.java:99) 
04-17 10:14:48.608: E/AndroidRuntime(11307):    at android.os.Looper.loop(Looper.java:143)
04-17 10:14:48.608: E/AndroidRuntime(11307):    at android.app.ActivityThread.main(ActivityThread.java:4196) 
04-17 10:14:48.608: E/AndroidRuntime(11307):    at java.lang.reflect.Method.invokeNative(Native Method) 
04-17 10:14:48.608: E/AndroidRuntime(11307):    at java.lang.reflect.Method.invoke(Method.java:507) 
04-17 10:14:48.608: E/AndroidRuntime(11307):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-17 10:14:48.608: E/AndroidRuntime(11307):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-17 10:14:48.608: E/AndroidRuntime(11307):    at dalvik.system.NativeStart.main(Native Method) 
04-17 10:14:48.608: E/AndroidRuntime(11307): Caused by: java.lang.RuntimeException: Could not create tab content because could not find view with id 2131099659
04-17 10:14:48.608: E/AndroidRuntime(11307):    at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:595)
04-17 10:14:48.608: E/AndroidRuntime(11307):    at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:586)
04-17 10:14:48.608: E/AndroidRuntime(11307):    at android.widget.TabHost$TabSpec.setContent(TabHost.java:441) 
04-17 10:14:48.608: E/AndroidRuntime(11307):    at org.jujitsu.app.com.TabViews.onCreate(TabViews.java:26) 
04-17 10:14:48.608: E/AndroidRuntime(11307):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
04-17 10:14:48.608: E/AndroidRuntime(11307):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
04-17 10:14:48.608: E/AndroidRuntime(11307):    ... 11 more

Not sure what I am doing wrong. I am a bit new to this.

UPDATE: Ok so.. What I mean is.. I want to change the Activity using tabs. Heres is how I did it. The answer was to set the content by Intent instead using tabHost.addTab(tabHost.newTabSpec("tab2") .setIndicator("Something") .setContent(new Intent(this, myActivity.class)));

Thankyou all those who helped. It made me consider more options.

Cheers, Joe.

Answer

m4n07 picture m4n07 · Apr 17, 2012

Your xml should have something like ,

  <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"
          >

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

    <TextView android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="test"/>   

    <TextView android:id="@+id/view2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="test"/>

    <TextView android:id="@+id/view3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="test"/>

    </FrameLayout>
    </TabHost>

examples: http://www.androidhive.info/2011/08/android-tab-layout-tutorial/ http://www.codeproject.com/Articles/107693/Tabbed-Applications-in-Android