There is a fragment of Fragment1 , with the help of the RecyclerView Button displayed in the adapter; it looks like this:

 @Override public void onBindViewHolder(final ViewHolder holder,final int position){ final Fragment1 fragment1 = new Fragment1(); final int id = (listItems.get(position).getId()); holder.button.setText(listItems.get(position).getName()); holder.button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: fragment1.replaceFragment(id); break; default: break; } } }); } 

The replaceFragment() method is registered in the Fragment1 class and is used to define the pressed button and replace Fragment1 with Fragment2 , Fragment3 , etc. It looks like this:

 public class Fragment1 extends Fragment{ private RecyclerView recyclerView; private RecyclerAdapter adapter; private FragmentTransaction fragmentTransaction; private List<RecyclerItem> listItems; private Context context; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.pb_layout,null); recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(context)); listItems = new ArrayList<>(); adapter = new RecyclerAdapter(listItems, getActivity()); recyclerView.setAdapter(adapter); return v; } public void replaceFragment(int id){ fragmentTransaction = getFragmentManager().beginTransaction(); switch (id){ case 1: fragmentTransaction.replace(R.id.containerView, new Fragment2()).commit(); fragmentTransaction.addToBackStack(null); break; case 2: fragmentTransaction.replace(R.id.containerView, new Fragment3()).commit(); fragmentTransaction.addToBackStack(null); break; default: break; } }} 

It seems to work but knocks out an error:

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentTransaction android.support.v4.app.FragmentManager.beginTransaction()' on a null object reference 

In the line: fragmentTransaction = getFragmentManager().beginTransaction();

Please tell me the reason.

  • What are your inheritance and fragment inherited from? - Yuriy SPb
  • @YuriiSPb aktiviti from AppCompatActivity, and a fragment from Fragment - Binary

1 answer 1

You create a snippet:

 Fragment1 fragment1 = new Fragment1(); 

After that, the fragment1 variable stores a link to the created fragment, which is not yet attached to any activations (this fragment does not have a host in the form of activations).

Next, you call the fragment method:

 fragment1.replaceFragment(id); 

In which, in particular, FragmentManager 's acquisition occurs:

 fragmentTransaction = getFragmentManager().beginTransaction(); 

The getFragmentManager() method of the Fragment class returns the FragmentManager activation, which is the host for this fragment.

Since this fragment is not associated with any activation, the getFragmentManager() method returns null and then a non-static method is called on the null object, which results in a NullPointerException .

In general, it is not very clear: if your RecyclerView is in the fragment Fragment1 , then why in the adapter do you create a new instance of Fragment1 ? Interact directly with the fragment in which the RecyclerView is located.

  • I do this to call the method located in Fragment1 in the adapter - Binary