我有显示片段的ViewPager:SearchFragment,FriendFragment。我正在努力做的是将SearchFragment更改为ChatFragment

replace-fragment-inside-a-viewpager/中有很多反馈,但是在使任何这些解决方案都起作用时我遇到了一些麻烦。

我认为我创建界面的方式可能有问题,我很难将其从ViewPager调用为SearchActivity.newInstance(new MyListener {...} )

我现在到达了


  java.lang.NullPointerException:尝试调用虚拟方法
  'android.support.v4.app.FragmentTransaction
  android.support.v4.app.FragmentManager.beginTransaction()'为null
  对象参考
                                                                                        在
  com.example.ashaneen.firebaseapp.adapters.MainScreenAdapter $ 1.onFragmentChange(MainScreenAdapter.java:44)
                                                                                        在
  com.example.ashaneen.firebaseapp.fragments.SearchFragment $ 4.onComplete(SearchFragment.java:132)


MainScreenAdapter.class

public class MainScreenAdapter extends FragmentPagerAdapter implements SearchFragment.MyListener {

    private static final String TAG = "MainScreenAdapter";

    private FragmentManager fragmentManager;
    private Fragment fragment1;


    public MainScreenAdapter(FragmentManager fm) {
        super(fm);
    }



    @Override
    public Fragment getItem(int position)
    {
        switch (position)
        {
            case 0:
                if (fragment1 == null)
                {
                    fragment1 = SearchFragment.newInstance(new SearchFragment.MyListener() {
                        @Override
                        public void onFragmentChange() {
                            fragmentManager.beginTransaction().remove(fragment1).commit();
                            fragment1 = ChatFragment.newInstance();
                            notifyDataSetChanged();
                        }
                    });
                }
                return fragment1;
            case 1:
                return new FriendsFragment();
            default:
                return new ChatFragment();
        }
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }
    @Override
    public void onFragmentChange() {

    }


SearchFragment.class

 public class SearchFragment extends android.support.v4.app.Fragment  {

        public SearchFragment() {
        }

        private String TAG = "Profile2Fragment";

        public interface MyListener
        {
             void onFragmentChange();
        }

        static MyListener myListener = new MyListener() {
            public void onFragmentChange() {

            }
        };

    public static SearchFragment newInstance(MyListener myListener) {

       SearchFragment.myListener = myListener;
       return new SearchFragment();
    }
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search, container, false);

        button = view.findViewById(R.id.btn_search);
        waitingRoomReference = db.collection("awaiting_room");




        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)

{
                    myListener.onFragmentChange();
                }
            }
        });

                return view;
            }

最佳答案

尝试改变这个

public MainScreenAdapter(FragmentManager fm) {
    super(fm);
}




public MainScreenAdapter(FragmentManager fm) {
    super(fm);
    fragmentManager = fm;
}

07-24 09:37