Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid 2.3.3 Tabbed Activity One View Per Tab | Receiving Error "Could not create tab content because could not find view with id"
    text
    copied!<p>Environment: <strong>Android 2.3.3 with Google API</strong></p> <p>I know this is kind of long, but I posted all information in case you needed to refer to it. Thanks in advance!</p> <p>I followed the Android example here: <a href="http://developer.android.com/guide/tutorials/views/hello-tabwidget.html" rel="nofollow">http://developer.android.com/guide/tutorials/views/hello-tabwidget.html</a>, but I am getting the error "Caused by: java.lang.RuntimeException: Could not create tab content because could not find view with id".</p> <p>Here is the app flow: The app begins at the home screen, and an Intent is created in onStart() and starts the CartActivity. Before the CartActivity is started, the app crashes with the RuntimeException "Could not create tab content because could not find view with id 2131034112"</p> <p>CartActivity</p> <pre><code>import com.cart.R; import android.app.Activity; import android.app.TabActivity; import android.os.Bundle; import android.util.Log; import android.widget.EditText; import android.widget.ListView; import android.widget.TabHost; import android.widget.TextView; public class CartActivity extends TabActivity { private TextView cartNameLabel; private EditText inputCartName; private TextView priceLabel; private EditText inputPrice; private EditText inputFruit; private ListView displayFruits; private TabHost tabHost; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.cart); Log.i(this.getClass().toString(), "Before addTabs()"); addTabs(); Log.i(this.getClass().toString(), "After addTabs()"); cartNameLabel = (TextView)findViewById(R.id.createCartNameLabel); inputCartName = (EditText)findViewById(R.id.enterCartName); priceLabel = (TextView)findViewById(R.id.createPriceLabel); inputPrice = (EditText)findViewById(R.id.enterPrice); inputFruit = (EditText)findViewById(R.id.enterFruit); } /** * Adds and displays the tabs to the Activity */ private void addTabs() { tabHost = getTabHost(); tabHost.addTab(tabHost.newTabSpec("Tab1+a").setIndicator("X").setContent(R.id.addTab1a)); tabHost.addTab(tabHost.newTabSpec("Tab2").setIndicator("Y").setContent(R.id.addTab2)); tabHost.setCurrentTab(0); } } </code></pre> <p>Cart.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="5dp"&gt; &lt;TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp"/&gt; &lt;!-- Layout for Add 1 + a Tab --&gt; &lt;LinearLayout android:id="@+id/addTab1a" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"&gt; &lt;TextView android:id="@+id/createCartNameLabel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/createCartNameLabelText"/&gt; &lt;EditText android:id="@+id/enterCartName" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;TextView android:id="@+id/createPriceLabel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/priceLabelText"/&gt; &lt;EditText android:id="@+id/enterPrice" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;/LinearLayout&gt; &lt;!-- Layout for Add Tab2 --&gt; &lt;LinearLayout android:id="@+id/addTab2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"&gt; &lt;TextView android:id="@+id/createFruitLabel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/createFruitLabelText"/&gt; &lt;EditText android:id="@+id/enterFruit" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; &lt;/TabHost&gt; </code></pre> <p>HomeActivity</p> <pre><code>package com.cart.activities; import com.cart.R; import com.cart.R.layout; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class HomeActivity extends Activity { private TextView welcomeMessage; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); welcomeMessage = (TextView)findViewById(R.id.welcomeMessage); } @Override public void onStart() { super.onStart(); Intent intent = new Intent(getApplicationContext(), CartActivity.class); HomeActivity.this.startActivity(intent); } } </code></pre> <p>main.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/welcomeMessage" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/welcome" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>R.java</p> <pre><code>/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */ package com.cart; public final class R { public static final class attr { } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class id { public static final int addTab1a=0x7f050000; public static final int addTab2=0x7f050005; public static final int createCartNameLabel=0x7f050003; public static final int createFruitLabel=0x7f050006; public static final int createPriceLabel=0x7f050001; public static final int enterCartName=0x7f050007; public static final int enterFruit=0x7f050002; public static final int enterPrice=0x7f050004; public static final int welcomeMessage=0x7f050008; } public static final class layout { public static final int cart=0x7f030000; public static final int main=0x7f030001; } public static final class string { public static final int app_name=0x7f040001; public static final int createCartNameLabelText=0x7f040002; public static final int createFruitLabelText=0x7f040004; public static final int priceLabelText=0x7f040003; public static final int welcome=0x7f040000; } } </code></pre> <p>Cart Manifest.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cart" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="10" /&gt; &lt;uses-permission android:name="android.permission.INTERNET"/&gt; &lt;uses-permission android:name="android.permission.CAMERA"/&gt; &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/&gt; &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/&gt; &lt;application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:debuggable="true"&gt; &lt;activity android:name=".activities.HomeActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name=".activities.CartActivity"&gt; &lt;intent-filter&gt;&lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload