2012-01-23 47 views
0

環境:的Android 2.3.3與谷歌APIAndroid 2.3.3選項卡式活動每個選項卡查看|收到錯誤「無法創建選項卡的內容,因爲找不到視圖ID爲」

我知道這是一種長,但我貼你需要的情況下,所有信息參考它。提前致謝!

我在這裏遵循Android示例:http://developer.android.com/guide/tutorials/views/hello-tabwidget.html,但我收到錯誤「由於:java.lang.RuntimeException:無法創建標籤內容,因爲無法找到帶有標識的視圖」。

以下是應用程序流程: 該應用程序從主屏幕開始,並在onStart()中創建Intent並啓動CartActivity。該CartActivity開始之前,與RuntimeException的應用程序崩潰

CartActivity 「不能因爲無法與ID 2131034112找到視圖中創建選項卡內容」

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; 
    } 
} 

車的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> 

回答

1

改變佈局文件來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: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 
      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="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="PriceLabelText"/> 

      <EditText 
      android:id="@+id/enterPrice" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"/> 
     </LinearLayout> 
     <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="CreateFruitLabelText"/> 

      <EditText 
      android:id="@+id/enterFruit" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"/> 
     </LinearLayout> 
    </FrameLayout> 
</LinearLayout> 
</TabHost> 
+0

謝謝,工作!順便說一句,對於那些正在查看此內容以及處於「tl; dr」時刻的人,在cart.xml中,我忘記了在「」的結尾之間添加「」。我意外地結束了FrameLayout的定義。 – Paradius

相關問題