2013-03-21 137 views
4

我有一個簡單的Android應用程序與選項卡設置使用Tabhost和碎片。在其中一個標籤頁上,該片段是一個列表視圖。我想要做的是,當用戶點擊列表視圖中的一個項目時,打開一個新視圖,其中包含基於點擊項目的更多特定信息,但保留它以便標籤仍然存在。Android的片段,沒有發現id內TabHost

我的方法是,當用戶在列表視圖中點擊一個項目時,創建一個新的片段,然後從擁有所有這些片段的主FragmentActivity類中處理標籤邏輯,它將分離當前顯示的片段和添加這個「新」(singleStationListItem)片段。我遇到的問題是,我無法將新片段添加到帶有R.id.realtab內容的fragmentTransaction中,因此未正確添加。我得到:

十月3日至21日:50:01.862:E/FragmentManager(1136):未發現ID 0x1010000(機器人:ATTR /主題)視圖中的片段singleStationListItem {40d090a0#2的ID = 0x1010000}

其中R.id.realtabcontent = 0x01010000;

我可以附加它沒有Id,但然後用戶無法返回(調用addtobackstash())。我已經搜索了這個錯誤,但其他解決方案不涉及到tabhost,我似乎做了什麼是必需的,比如在onCreate()中調用setContextView(...)到包含我的framelayout標籤的佈局的xml文件試圖附加我的片段。我可以將標籤片段添加到R.id.realtab內容中,而不會出現問題。有誰知道什麼可能導致我的錯誤,我怎麼能解決它?

注意:我遵循本教程以獲取我的選項卡的工作。我的代碼非常相似。 http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/

我的代碼: activiy_main.xml從我的片段活動類

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/main_body" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

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

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="0dp" 
       android:layout_height="0dp" 
       android:layout_weight="0" 
       android:padding="5dp" /> 

      <FrameLayout 
       android:id="@+android:id/realtabcontent" <--id that I want to set to 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" /> 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="-4dp" 
       android:layout_weight="0" 
       android:orientation="horizontal" /> 
     </LinearLayout> 
    </TabHost> 
</LinearLayout> 

main_activity.java //部分

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Step 1: Inflate layout 
     setContentView(R.layout.activity_main); 
     // Step 2: Setup TabHost 
     initialiseTabHost(savedInstanceState); 
     if (savedInstanceState != null) { 
      mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state 
     } 
    } 

public void onTabChanged(String tag) { //handles tab changes 
    TabInfo newTab = (TabInfo) this.mapTabInfo.get(tag); 
    if (mLastTab != newTab) { 
     FragmentTransaction ft =  this.getSupportFragmentManager().beginTransaction(); 
     if (mLastTab != null) { 
      if (mLastTab.fragment != null) { 
       ft.detach(mLastTab.fragment); 
      } 
     } 
     if (newTab != null) { 
      if (newTab.fragment == null) { 
       newTab.fragment = Fragment.instantiate(this, 
         newTab.clss.getName(), newTab.args); 
       ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag); //note it uses the R.id.realtabcontent without any issues here 
      } else { 
       ft.attach(newTab.fragment); 
      } 
     } 

     Log.d("fragment manager in activity: ", "" + ft.isEmpty()); 
     mLastTab = newTab; 
     ft.commit(); 
     this.getSupportFragmentManager().executePendingTransactions(); 
     Log.d("ft ID: ", ft.toString()); 
    } 
} 

//This is the callback function inside the listview fragment. I created an interface and it is overwritten in here as suggested by the Android SDK guide 
public void onStationSelected(String station) { 
    FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction(); 
    ft.addToBackStack(null); 
    ft.addToBackStack(null); 

    //create argument for new fragment that will be created 
    Bundle b = new Bundle(); 
    b.putString("station", station); 
    Fragment displayStationInfo = Fragment.instantiate(this, singleStationListItem.class.getName(), b); 

    Fragment cur = getSupportFragmentManager().findFragmentById(R.id.realtabcontent); 
    ft.detach(cur); 
    ft.add(R.id.realtabcontent, displayStationInfo); <-- this line causes the error 
    ft.commit(); 
    this.getSupportFragmentManager().executePendingTransactions(); 
} 

singleStationListItem.java在這裏

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Bundle b = this.getArguments(); 

    getActivity().setContentView(R.layout.single_station_item_view); 

    TextView txtStation = (TextView) getActivity().findViewById(R.id.station_label); 

    String station = b.getString("station"); 
    // displaying selected product name 
    Log.d(TAG, "passed in station information: " + station); 
    txtStation.setText(station); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View v = (LinearLayout)inflater.inflate(R.layout.single_station_item_view, container, false); 
    return v; 
} 

任何幫助將是gre非常感謝。

回答

0

我意識到這個問題前一個問題,我發佈了一個可能會遇到這個問題的其他人的答案。

據我所知,沒有'realtabcontent'的Android ID。在你的XML中,刪除你的id的'android'部分爲FrameLayout

<FrameLayout 
    android:id="@+id/realtabcontent" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" /> 

貼在developer documentation完整的XML代碼如下:

<android.support.v4.app.FragmentTabHost 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

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

    <TabWidget 
     android:id="@android:id/tabs" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"/> 

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="0"/> 

    <FrameLayout 
     android:id="@+id/realtabcontent" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 

</LinearLayout> 

好運和快樂編碼!