0

So I am trying to pass text in one fragment to main activity, and have main activity pass it to another fragment so it can be displayed.

Here is the code of the fragment to send it to main activity:

public class TopFragment extends Fragment{
  private static EditText topInput;
  private static EditText bottomInput;

  FragmentInterface communicate;

  public interface FragmentInterface{
    public void sendTheInput(String topText,String bottomText);
  }

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.top_fragment,container,false);

    topInput = (EditText) view.findViewById(R.id.topInput);
    bottomInput = (EditText) view.findViewById(R.id.bottomInput);
    final Button submitButton = (Button)view.findViewById(R.id.submitButton);

    submitButton.setOnClickListener(
            new View.OnClickListener(){
                public void onClick(View v){
                    buttonClicked(v);
                }
        }
    );
    return view;
  }

  public void buttonClicked(View view){
   communicate.sendTheInput(topInput.getText().toString(),bottomInput.getText().toString());
  }
}

Here is the bit of code in main activity:

@Override
public void sendTheInput(String topText, String bottomText) {
  BottomFragment bottomFrag = (BottomFragment)getSupportFragmentManager().findFragmentById(R.id.bottomFragment);
        bottomFrag.setBoxText(topText,bottomText);
}

And here is the code of the fragment that will display the text:

public class BottomFragment extends Fragment{

  private static EditText topTextBox;
  private static EditText bottomTextBox;

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

  public void setBoxText(String topText, String bottomText){

    topTextBox.setText(topText);
    bottomTextBox.setText(bottomText);
  }
}

When I run the app I have no runtime errors but when I click the button in me emulator I get this:

08-07 13:20:48.125    4011-4011/com.example.vanessaanthony.fragmentreview E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.vanessaanthony.fragmentreview, PID: 4011
    java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.vanessaanthony.fragmentreview.TopFragment$FragmentInterface.sendTheInput(java.lang.String, java.lang.String)' on a null object reference
            at com.example.vanessaanthony.fragmentreview.TopFragment.buttonClicked(TopFragment.java:44)
            at com.example.vanessaanthony.fragmentreview.TopFragment$1.onClick(TopFragment.java:35)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Why am I getting this error message?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Ryan Anthony
  • 21
  • 1
  • 3

1 Answers1

2

You just forgot to set communicate in your fragment. I'd recommend doing it in onAttach

public class TopFragment extends Fragment
{
    private static EditText topInput;
    private static EditText bottomInput;

    FragmentInterface communicate;

    public interface FragmentInterface{
        public void sendTheInput(String topText,String bottomText);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.top_fragment,container,false);

        topInput = (EditText) view.findViewById(R.id.topInput);
        bottomInput = (EditText) view.findViewById(R.id.bottomInput);
        final Button submitButton = (Button)view.findViewById(R.id.submitButton);

        submitButton.setOnClickListener(
                new View.OnClickListener(){
                    public void onClick(View v){
                        buttonClicked(v);
                    }
            }
        );
        return view;
    }

    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        communicate = (FragmentInterface) activity;
    }

    public void buttonClicked(View view){

        communicate.sendTheInput(topInput.getText().toString(),bottomInput.getText().toString());

    }   
}

Make sure that your activity implements FragmentInterface

d0nut
  • 2,835
  • 1
  • 18
  • 23