0

我想在另一個片段內單擊一個對話框時打開一個片段。我在Tab中使用了ActionBarSherlock。我的片段附加在視圖尋呼機中。我幾乎完成了這項工作。但我不能在視圖尋呼機中替換一個新的片段。我有一個錯誤。我讀了線程here。解決方案尚不清楚。當單擊對話框時將另一個片段替換爲另一個片段(viewpager)

錯誤:

10-18 21:34:40.379: E/AndroidRuntime(19618): FATAL EXCEPTION: main 10-18 21:34:40.379: E/AndroidRuntime(19618): java.lang.IllegalArgumentException: No view found for id 0x7f040032 (com.example.actionbartestwithsherlock:id/pager) for fragment AllContactsFragment{41fd4ba0 #0 id=0x7f040032} 10-18 21:34:40.379: E/AndroidRuntime(19618): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)

我有尋呼機命名FragmentTab1,FragmentTab2 & FragmentTab3 3組片段的同事。

MainActivity & FragmentAdapter看起來象下面這樣:

public class MainActivity extends SherlockFragmentActivity { 

    ActionBar.Tab Tab1, Tab2, Tab3, Tab4; 
    private Context context = this; 
    // view pager 
    // Declare Variables 
    ActionBar actionBar; 
    ViewPager mPager; 
    Tab tab; 
    FragmentAdapter mAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     // set application in portrait mode 
     ActivityHelper.initialize(this); 

     actionBar = getSupportActionBar(); 
     actionBar.setDisplayShowHomeEnabled(true); 
     actionBar.setDisplayShowTitleEnabled(true); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Locate ViewPager in activity_main.xml 
     mPager = (ViewPager) findViewById(R.id.pager); 
     // add an adapter to pager 
     mPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(), 
       mPager, actionBar)); 

     addActionBarTabs(); 
    } 

    private void addActionBarTabs() { 

     String[] tabs = { "Tab 1", "Tab 2", "Tab 3" }; 
     for (String tabTitle : tabs) { 
      ActionBar.Tab tab = actionBar.newTab().setText(tabTitle) 
        .setTabListener(tabListener); 
      actionBar.addTab(tab); 
     } 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    } 

    private ActionBar.TabListener tabListener = new ActionBar.TabListener() { 
     @Override 
     public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { 
      mPager.setCurrentItem(tab.getPosition()); 
     } 

     @Override 
     public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) { 
     } 

     @Override 
     public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) { 
     } 
    }; 

class FragmentAdapter extends FragmentPagerAdapter implements 
      ViewPager.OnPageChangeListener { 

     private ViewPager mViewPager; 
     final int TOTAL_PAGES = 3; 

     public FragmentAdapter(FragmentManager fm, ViewPager pager, 
       ActionBar actionBar) { 
      super(fm); 
      this.mViewPager = pager; 
      this.mViewPager.setOnPageChangeListener(this); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      switch (position) { 
      case 0: 
       return FragmentTab1.newInstance(); 
      case 1: 
       return FragmentTab2.newInstance(); 
      case 2: 
       return FragmentTab3.newInstance(); 
      default: 
       throw new IllegalArgumentException(
         "The item position should be less or equal to:" 
           + TOTAL_PAGES); 
      } 
     } 

     @Override 
     public int getCount() { 
      return TOTAL_PAGES; 
     } 

     @Override 
     public void onPageScrollStateChanged(int arg0) { 

     } 

     @Override 
     public void onPageScrolled(int arg0, float arg1, int arg2) { 

     } 

     @Override 
     public void onPageSelected(int position) { 
      actionBar.setSelectedNavigationItem(position); 
     } 
    } 
} 

現在,在我的第一個標籤FragmentTab1,我打開一個自定義對話框時,點擊一個按鈕。當選擇對話框選項時,我想在FragmentTab1中替換新的片段AllContactsFragment

FragmentTab1片段類:

public class FragmentTab1 extends SherlockFragment implements OnClickListener { 

Button btnTest; 
ViewPager pager; 
LinearLayout layoutBlockNumbers; 
LinearLayout layoutContact, layoutCallLog, layoutSMSLog, layoutManually; 
Context context; 
CustomizedDialog dialog; 
private static final int CONTACT_PICKER_RESULT = 1001; 
private static final String DEBUG_TAG = "Contact List"; 
private static final double RESULT_OK = -1; 

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

    View rootView = inflater.inflate(R.layout.fragmenttab1, container, 
      false); 

    layoutBlockNumbers = (LinearLayout) rootView 
      .findViewById(R.id.layoutAddBlockNumbers); 
    layoutBlockNumbers.setOnClickListener(this); 
    return rootView; 

} 

@Override 
public void onClick(View v) { 

    if (v == layoutCallLog) { 

     dialog.dismiss(); 
     // want to replace new fragment at position 0 in pager 
     // problem is here ??? how to open new fragmnet 
     Fragment allContactsFragment = AllContactsFragment.newInstance(); 
     FragmentTransaction transaction = getChildFragmentManager() 
       .beginTransaction(); 
     transaction.addToBackStack(null); 
     transaction.replace(R.id.pager, allContactsFragment).commit(); 

    } 

    if (v == layoutBlockNumbers) { 
     // open a dialog 
     showDialog(); 
    } else if (v == layoutContact) { 
     openContactList(); 
     dialog.dismiss(); 
    } else if (v == layoutSMSLog) { 
    } 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    setUserVisibleHint(true); 
} 

// open a dialog 
private void showDialog() { 
    dialog = new CustomizedDialog(getActivity()); 
    dialog.setContentView(R.layout.dialog_add_number_type); 
    dialog.setTitle("Add Black List Number"); 

    //initialize all linear layouts in dialog 
    layoutCallLog = (LinearLayout) dialog.findViewById(R.id.layoutCallLog); 
    layoutContact = (LinearLayout) dialog.findViewById(R.id.layoutContact); 
    layoutSMSLog = (LinearLayout) dialog.findViewById(R.id.layoutSMSLog); 
    layoutManually = (LinearLayout) dialog 
      .findViewById(R.id.layoutManually); 

    // add listener to several linear layout 
    layoutContact.setOnClickListener(this); 
    layoutCallLog.setOnClickListener(this); 
    layoutSMSLog.setOnClickListener(this); 
    layoutManually.setOnClickListener(this); 

    dialog.show(); 
} 

public static Fragment newInstance() { 
    Fragment f = new FragmentTab1(); 
    return f; 
} 
} 

activity_main.xml中看起來象下面這樣:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 
    </android.support.v4.view.ViewPager> 

</RelativeLayout> 

能有人能幫助我解決這個問題?很抱歉,海量代碼。

回答

1

我不確定你可以按照你想要的方式做事。 A ViewPager的設置方式與正常的容器/片段設置不同。在ViewPager中,您沒有使用片段事務來添加片段,而是使用從備份列表中加載片段實例的適配器。

更換片段,然後將工作如下:

(1)創建要添加 (2)片段添加到正在備份您的PagerAdapter (3)顯示列表中的片段的實例新片段 (4)刪除舊版本

在您當前的項目中實現此功能的問題是您的適配器的設置問題。目前,您正在使用只能返回固定數量片段的開關狀態。你的適配器應該像這樣設置。

class MyPageAdapter extends FragmentPagerAdapter{ 
    private List<Fragment> fragments 
    public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) { 
     super(fm); 
     this.fragments = fragments; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return this.fragments.get(position); 
    } 

    @Override 
    public int getCount() {  
     return this.fragments.size(); 
    } 
} 

然後,您可以添加一個方法到您的適配器類來添加或刪除新的片段。如果你知道你想要替換的片段的索引,這應該很容易。您只需創建聯繫人片段的新實例,將其添加到您的數組或列表中即可。 This Post說明ViewPager如何處理添加/刪除新內容以及如何確保顯示新片段。

+0

感謝您快速回答。我正在檢查.. –

+0

其中我在列表中添加片段?在我的場景中添加如何?我很困擾。認爲我是一名初級程序員。 –

+0

我已經添加了舊的片段列表,它運作良好。但我的問題是如何在對話框onclick事件中打開新片段?@Rarw –

0

當我讀到this後,我解決了答案。

我只是在我的fragmenttab1.xml的頂部佈局中添加了一個ID android:id =「@ + id/fragmentTabLayout1。然後像往常一樣撥打 新片段:

Fragment allContactsFragment = AllContactsFragment.newInstance(); 
     FragmentTransaction transaction = getChildFragmentManager() 
       .beginTransaction(); 
     transaction.addToBackStack(null); 
     // use this id to replace new fragment 
     transaction.replace(R.id.fragmentTabLayout1, allContactsFragment).commit(); 
相關問題