viewPagers片段的findFragmentByTag始终返回null

时间:2016-12-06 13:27:19

标签: android android-fragments

我正在尝试从我的Fragment position 0ViewPager审核NullPointerException,我认为我已经做好了一切,但我仍然得到MainActivity

在我的Fragment我尝试使用代码检索GaitDistribution gaitDistribution = (GaitDistribution) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.topPager + ":" + 0);

FragmentPagerAdapater

快速查看private static String makeFragmentName(int viewId, long id) { return "android:switcher:" + viewId + ":" + id; } 类告诉我们适配器为它所拥有的每个片段创建片段标记:

viewID

R.id.topPager参数是viewId,但无论如何我还是试着看看FragmentPagerAdapter真正传递了什么,所以我在@Override public Object instantiateItem(ViewGroup container, int position) { Log.d("TAG", container.getId() + " pos: " + position); return super.instantiateItem(container, position); } 类中尝试了这个

2131493099

Logcat给了我viewId作为R.id.topPager,并将该整数放在标记字符串而不是public class MyHorseViewPagerAdapter extends FragmentPagerAdapter { private static final int NUMofPages = 3; private int walkProgess, canterProgress, trotProgress; private int duration; private boolean isSound, isVibrate; public MyHorseViewPagerAdapter(FragmentManager fm, int walkProgess, int canterProgress, int trotProgress, int duration, boolean isSound, boolean isVibrate) { super(fm); this.walkProgess = walkProgess; this.canterProgress = canterProgress; this.trotProgress = trotProgress; this.duration = duration; this.isSound = isSound; this.isVibrate = isVibrate; } public MyHorseViewPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position) { case 0: GaitDistribution gaitDistribution = GaitDistribution.newInstance(walkProgess, trotProgress, canterProgress, isSound, isVibrate, duration); return gaitDistribution; case 1: Intensity intensity = new Intensity(); intensity.setRetainInstance(true); return intensity; case 2: Stats stats = new Stats(); return stats; default: return null; } } @Override public int getCount() { return NUMofPages; } } 中,但没有成功!

ViewPager适配器:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Switch(position) {
                   case 0:
                         //get first subcategories array values
                         break;
                   case 1:
                         //get second subcategories array values
                         break;
                   ....
                }
            }

0 个答案:

没有答案