[英]getSupportFragmentManager().findFragmentById returns null with FragmentPagerAdapter and ViewPager
I checked all the similar tickets with "getSupportFragmentManager().findFragmentById returns null" but none helped. 我用“ getSupportFragmentManager()。findFragmentById返回null”检查了所有类似的票证,但没有帮助。
In Android Studio 3.0.1 I just created a new project based on the "Tabbed Activity" template project and navigation style set to "Action Bar Tabs (with ViewPager)". 在Android Studio 3.0.1中,我刚创建了一个基于“ Tabbed Activity”模板项目的新项目,并将导航样式设置为“ Action Bar Tabs(with ViewPager)”。
Then I added 3 xml files 然后我添加了3个xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment1">
<!-- @+id/fragment2 in fragment2.xml-->
<!-- @+id/fragment3 in fragment3.xml-->
...some controls
</LinearLayout>
And the 3 corresponding .java class files 以及3个对应的.java类文件
public class Fragment1 extends Fragment {
//Fragment2, Fragment3
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
//R.layout.fragment2, R.layout.fragment3
}
}
In the SectionsPagerAdapter class of MainActivity class, I changed the overriden method getItem to the following. 在MainActivity类的SectionsPagerAdapter类中,我将重写方法getItem更改为以下内容。
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch(position) {
case 0: return new Fragment1();
case 1: return new Fragment2();
case 2: return new Fragment3();
default:
return new Fragment1();
}
//return PlaceholderFragment.newInstance(position + 1);
}
And finally, I want to trigger something in my first fragment. 最后,我想在我的第一个片段中触发一些东西。
public void onClick(View view) {
Fragment1 frag1 = (Fragment1) getSupportFragmentManager().findFragmentById(R.id.fragment1);
}
My problem is that when I click a button in the toolbar and I reach its onClick event (in MainActivity), my frag1 is always null! 我的问题是,当我单击工具栏上的按钮并达到其onClick事件(在MainActivity中)时,我的frag1始终为null!
Please help! 请帮忙! :)
:)
you shouldn't override getItem like this way 你不应该这样覆盖getItem
I suggest to be like: 我建议像这样:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public SectionsPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
public void setFragments(List<Fragment> fragments) {
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
and if you want to get any fragment you want just call 如果您想获取任何片段,只需致电
((FragmentCallBack)sectionsPagerAdapter.get(thePositionOfYourFragment))
.trigger();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.