为什么我一直在Android Studio中获得java.lang.nullpointerexception?

时间:2015-08-07 18:23:27

标签: java android android-fragments nullpointerexception

所以我试图将一个片段中的文本传递给主要活动,并将主要活动传递给另一个片段,以便显示它。

以下是将其发送到主要活动的片段代码:

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());
  }
}

以下是主要活动中的一些代码:

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

以下是将显示文本的片段代码:

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);
  }
}

当我运行应用程序时,我没有运行时错误,但是当我点击模拟器中的按钮时,我得到了这个:

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)

为什么我收到此错误消息?

1 个答案:

答案 0 :(得分:2)

您忘了在片段中设置communicate。我建议您在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());

    }   
}

确保您的活动implements FragmentInterface