2013-03-21 162 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> 

好运和快乐编码!