从标签片段

时间:2016-02-07 09:43:45

标签: android android-viewpager fragment android-tabhost

我有一个FragmentMain类,扩展Fragment实现TabHost。

public class FragmentNews extends Fragment implements TabHost.OnTabChangeListener {

在FragmentMain中我有三个不同的Fragment选项卡(FragmentA,FragmentB,FragmentC)都扩展了Fragment。

从FragmentA我想在点击按钮时加载新的FragmentNews。单击按钮后运行应用程序时出现错误并关闭应用程序。这段代码:

FragmentA类

        public class FragmentMain extends Fragment {
         private Button mbuton;

         public MyNewsFragment() {

            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                // Inflate the layout for this fragment
                View myNews = inflater.inflate(R.layout.fm_my_news, container, false);

               mbuton= (Button)myNews.findViewById(R.id.bttest);
                mbuton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                       FragmentNews fragment = new FragmentNews();

                        getFragmentManager().beginTransaction()
                                .replace(R.id.fragment_container, fragment)
                                .commit();
    .....
return (myNews);
    }

请注意,当FragmentA不在选项卡中时,此工作。

My FragmentMain包含类似内容:

FragmentMain类

public class FragmentMain extends Fragment implements TabHost.OnTabChangeListener {
    ViewPager viewPager;
    TabHost tabHost;
    View vnews;


    public FragmentMain () {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View newshow = inflater.inflate(R.layout.fm_news_layout, container, false);
        ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Actualités");
        vnews= initViewPager(newshow);

        initTabHost(newshow);

        return (vnews);
    }

    private View initViewPager(View newshow){

        viewPager = (ViewPager)newshow.findViewById(R.id.view_pagerr);

        List<Fragment> listFragments = new ArrayList<Fragment>();
        listFragments.add(new FragmentA());
        listFragments.add(new FragmentB());
...

        MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(
                getChildFragmentManager(), listFragments);//getChildFragmentManager instead getFragmentManager to avoid Tab content disappeared after change page
        viewPager.setAdapter(myFragmentPagerAdapter);

我的MyFragmentPagerAdapter类

public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

    List<Fragment> listFragments;

    public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> listFragments) {
        super(fm);
        this.listFragments = listFragments;
    }

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

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

我是新的Android所以请帮助!!!!

2 个答案:

答案 0 :(得分:0)

尝试 getChildFragmentManager()而不是 getFragmentManager()

答案 1 :(得分:0)

这是我在Logcat verbose

中得到的
02-08 11:24:34.768 1304-1323/? I/ActivityManager: Displayed com.graceform.mds.bvrmnewdemo/.MainEntry: +1s46ms
02-08 11:24:39.111 1304-1316/? I/ActivityManager: START u0 {cmp=com.graceform.mds.bvrmnewdemo/.MainActivity (has extras)} from uid 10057 on display 0
02-08 11:24:41.668 1304-1323/? I/ActivityManager: Displayed com.graceform.mds.bvrmnewdemo/.MainActivity: +2s508ms
02-08 11:24:55.909 1304-1801/? W/ActivityManager:   Force finishing activity com.graceform.mds.bvrmnewdemo/.MainActivity

                                                  [ 02-08 11:24:55.926  1304: 1801 D/         ]
                                                  HostConnection::get() New Host Connection established 0x9bb47c60, tid 1801
02-08 11:24:56.507 1304-1318/? W/ActivityManager: Activity pause timeout for ActivityRecord{9c508d4 u0 com.graceform.mds.bvrmnewdemo/.MainActivity t250 f}
02-08 11:24:59.921 1304-1316/? I/WindowState: WIN DEATH: Window{af4bebe u0 com.graceform.mds.bvrmnewdemo/com.graceform.mds.bvrmnewdemo.MainActivity}

由于