Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android viewPagers片段的findFragmentByTag始终返回null_Android_Android Fragments - Fatal编程技术网

Android viewPagers片段的findFragmentByTag始终返回null

Android viewPagers片段的findFragmentByTag始终返回null,android,android-fragments,Android,Android Fragments,我正试图从我的ViewPager中检索位置0处的片段,我认为我做的一切都是对的,但我仍然得到NullPointerException 在我的main活动中我尝试用代码检索片段: GaitDistribution gaitDistribution = (GaitDistribution) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.topPager + ":" + 0); public clas

我正试图从我的
ViewPager
中检索
位置0处的
片段,我认为我做的一切都是对的,但我仍然得到
NullPointerException

在我的
main活动中
我尝试用代码检索
片段

GaitDistribution gaitDistribution = (GaitDistribution) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.topPager + ":" + 0);
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;
    }
}
快速查看
FragmentPageRadapter
类,我们会发现适配器通过以下方式为其持有的每个片段创建片段标记:

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);
}
Logcat给了我:
2131493099
作为
viewId
,并将该整数放入标记字符串中,而不是
R.id.topPager
,但没有成功

ViewPager适配器:

GaitDistribution gaitDistribution = (GaitDistribution) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.topPager + ":" + 0);
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;
    }
}

是否要在某个位置获取片段?在哪里将
片段添加到
查看页面
?请添加该代码。您不应重写私有方法。@MuratK的可能副本保留对viewpager中所有片段的引用是非常昂贵的内存。是否要在某个位置获取片段?您在哪里将
片段添加到
viewpager
?请添加该代码。您不应该重写私有方法。保留对viewpager中所有片段的引用的@MuratK的可能重复是非常昂贵的内存开销