Environment: Android 2.3.3 with Google API
I know this is kind of long, but I posted all information in case you needed to refer to it. Thanks in advance!
I followed the Android example here: http://developer.android.com/guide/tutorials/views/hello-tabwidget.html, but I am getting the error “Caused by: java.lang.RuntimeException: Could not create tab content because could not find view with id”.
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”
CartActivity
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);
}
}
Cart.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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp">
<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"
android:padding="5dp"/>
<!-- Layout for Add 1 + a Tab -->
<LinearLayout
android:id="@+id/addTab1a"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/createCartNameLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/createCartNameLabelText"/>
<EditText
android:id="@+id/enterCartName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/createPriceLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/priceLabelText"/>
<EditText
android:id="@+id/enterPrice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<!-- Layout for Add Tab2 -->
<LinearLayout
android:id="@+id/addTab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/createFruitLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/createFruitLabelText"/>
<EditText
android:id="@+id/enterFruit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</TabHost>
HomeActivity
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);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/welcomeMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/welcome" />
</LinearLayout>
R.java
/* 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;
}
}
Cart Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cart"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:debuggable="true">
<activity
android:name=".activities.HomeActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.CartActivity">
<intent-filter></intent-filter>
</activity>
</application>
</manifest>
Change the name of the Layout file to cart.xml
When changed to the following format your sample runs