2014-02-06 44 views
1

我有一個查看分頁器,它有3個由FragmentActivity託管的選項卡片段。ViewPager沒有查看ID的片段

我所試圖做的是單擊某個項目後,用一個新的來代替那些片段:

public class UserShopActivity extends FragmentActivity implements 
     UserItemsFragment.OnUserItemSelectedListener, 
     UserCategoriesFragment.OnUserCategoriesSelectedListener, 
     UserSelectedCategoryFragment.OnUserItemsSelectedListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //setContentView(R.layout.activity_user_profile); 

     mViewPager = new ViewPager(this); 
     mViewPager.setId(R.id.shop_pager); 
     setContentView(mViewPager); 
} 

... 

    @Override 
    public void onUserCategoriesSelected(String category_id) { 
     UserSelectedCategoryFragment fragment = new UserSelectedCategoryFragment(); 
     getSupportFragmentManager().beginTransaction() 
       .replace(R.id.shop_container, fragment) 
       .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) 
       .addToBackStack(null) 
       .commit(); 
} 
} 

但是我得到這個錯誤:

java.lang.IllegalArgumentException: No view found for id 0x7f070011 (com.ked.ai:id/shop_container) for fragment UserSelectedCategoryFragment{40f7bcf0 #3 id=0x7f070011}

我已經設置了容器的id在的setContentView viewpager這是shop_container

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/shop_pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <FrameLayout 
     android:id="@+id/shop_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</android.support.v4.view.ViewPager> 

卜它找不到該ID。我該如何正確地更換它?

回答

1

您不需要替換ViewPager中的片段即可實現FragmentPagerAdapterFragmentStatePagerAdapter以在視圖尋呼機中顯示片段。

欲瞭解更多信息:

http://developer.android.com/training/animation/screen-slide.html

嘗試更改爲:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/shop_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

<android.support.v4.view.ViewPager 
    android:id="@+id/shop_pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 




</android.support.v4.view.ViewPager> 
</FrameLayout> 

變化onCreate到:

public class UserShopActivity extends FragmentActivity implements 
     UserItemsFragment.OnUserItemSelectedListener, 
     UserCategoriesFragment.OnUserCategoriesSelectedListener, 
     UserSelectedCategoryFragment.OnUserItemsSelectedListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //setContentView(R.layout.activity_user_profile); 
     setContentView(R.layout.yourlayoutname); 
     mViewPager = (ViewPager)findViewById(R.id.shop_pager); 


} 
+0

我已經有顯示這些3個標籤。現在我正試圖在viewpager之外調用一個新的片段。這可能嗎?或者我有其他選擇嗎? – AimanB

+0

所以把framelayout出視圖尋呼機。 –

+0

依然如此。我編輯了這個問題。請檢查一下。有什麼我錯過了嗎? – AimanB