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的可能重复是非常昂贵的内存开销