3

I want to call the Fragment Method when I click on the FloatingActionButton.
I am using Butterknife.

Error:

PID: 9956 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.fischerdaniel.testapp.FirstFragment.setTvText()' on a null object reference

The part of the MainActicity:

  @OnClick({R.id.fab_one, R.id.fab_two, R.id.fab_three}) public void test(View view) {
        switch (view.getId()) {
            case R.id.fab_three:
                FirstFragment firstFragment = (FirstFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_first);
                firstFragment.setTvText();
                break;
            default:
                break;
        }
    }

The part of the Fragment:

    @BindView(R.id.et_one) EditText etOne;
    @BindView(R.id.et_two) EditText etTwo;
    @BindView(R.id.tv_one) TextView tvOne;
    @BindView(R.id.tv_two) TextView tvTwo;

    private Unbinder unbinder;

   @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first, container, false);
        unbinder = ButterKnife.bind(this, view);

        return view;
    }

    protected void setTvText() {
        tvOne.setText(etOne.getText());
        tvTwo.setText(etTwo.getText());
    }
Pang
  • 9,564
  • 146
  • 81
  • 122

3 Answers3

1

See this link: findFragmentById always returns null

When you create your fragment, use:

add(int id, Fragment fragment, String tag) 

and then to get your fragment, use:

findFragmentByTag(String tag)
Community
  • 1
  • 1
Caleb
  • 44
  • 5
0
public void onMenuItemClick(View clickedView, int position) {
    switch (position){
        case 1:
             fragmentChanger("Notifications",new notifyFragment());
           break;
        case 2:
             fragmentChanger("QR Code",new qrFragment());
            break;
        case 3:
            fragmentChanger("User ID",new MainFragment());
            break;
        case 4:
            fragmentChanger("Settings",new settingsFragment());
            break;
        default:
            Toast.makeText(this, "Clicked on position: " + position, Toast.LENGTH_SHORT).show();
            break;
    }
}

//function that does the trasaction when a fragment object is passed
//R.id.container is my default frame
public void fragmentChanger(String title, Object expectedFragment){
   // mToolBarTextView.setText(title);
    transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.container, (Fragment) expectedFragment);
    transaction.addToBackStack(null);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    transaction.commit();
}
Kevin Muchwat
  • 1,445
  • 18
  • 18
0

Call your fragment method from Activity in like below,

it worked for me

FirstFragment firstFragment =(FirstFragment)getSupportFragmentManager().getFragments().get(0); 

firstFragment.yourmethod();

Here 0 is your fragment position.

Nensi Kasundra
  • 1,980
  • 6
  • 21
  • 34