Create a class instance

public class AddFragment extends Fragment { private DataBaseHelper myDb; private Button writeButton, deleteButton; private EditText namePassEdit, emailEditText, pasEditText, informationEditText; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.new_add_fragment, null); initializeViews(rootView); return rootView; } private void initializeViews(View rootView){ namePassEdit = rootView.findViewById(R.id.nameEditText); emailEditText = rootView.findViewById(R.id.emailEditText); pasEditText = rootView.findViewById(R.id.passwodEditText); informationEditText = rootView.findViewById(R.id.infoEditText); writeButton = rootView.findViewById(R.id.writeButton); /*deleteButton = rootView.findViewById(R.id.delButton); deleteButton.setVisibility(rootView.GONE);*/ writeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PassClass passClass = new PassClass(); passClass.setName(namePassEdit.getText().toString()); passClass.setEmail(emailEditText.getText().toString()); passClass.setPassword(pasEditText.getText().toString()); passClass.setInfo(informationEditText.getText().toString()); if (new DBAdapter(getActivity()).savePass(passClass)){ Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getActivity(), "Not Saved", Toast.LENGTH_SHORT).show(); } } }); } public void writeToSql(){ } @Override public void onDestroyView() { super.onDestroyView(); }} 

There is an identical fragment that is created without problems, fragments are called via a common function.

 private void setFragment(Fragment fragment) { FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.main_frame, fragment); fragmentTransaction.commit(); } 

I looked in debug mode, I saw the following. I think the problem is this. What it is and how to get rid of it debug mode

    1 answer 1

    See what value you are passing to the variable.

     Fragment fragment 

    in the method

     private void setFragment(Fragment fragment){} 

    and compare with the variable that you pass when you call setFragment (), where this method works correctly. In general, you should transmit a new instance instance -

     setFragment(new YourFragmentName()); 

    Ps Complete the FragmentTransaction like this:

     FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.main_frame, fragment); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); ft.addToBackStack(null); ft.commit();