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?

5
  • what is line no 44 in TopFragment.java? Commented Aug 7, 2015 at 18:25
  • 1
    Your communicate object is null. Check stackoverflow.com/questions/218384/… Commented Aug 7, 2015 at 18:26
  • did you implement your fragmentinterface in the main activity? Commented Aug 7, 2015 at 18:26
  • the stacktrace clearly says that communicate is null. Commented Aug 7, 2015 at 18:28
  • What is communicate ? Commented Aug 7, 2015 at 18:30

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

@Codebender The activity this fragment is placed in should be implementing FragmentInterface which will allow the cast
@Codebender happy to hear that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.