'Attempt to invoke interface method on a null object reference' 在 Fragment 之间进行通信

'Attempt to invoke interface method on a null object reference' in communicating between Fragments

我正在尝试构建一个示例应用程序,它在同一 activity 中包含两个片段。一个片段有 EditText、SeekBar 和一个按钮。第二个片段有一个 TextView。在第一个片段的 EditText 中输入文本并将 seekbar 设置为适当的值并单击按钮将更改第二个片段中的文本,其中文本在第一个片段中输入,大小由 seekbar 描述。我写了一个很好的代码,但应用程序在单击按钮时一直崩溃。请帮忙!

片段一:


import ....

public class Toolbarfragment extends Fragment implements OnSeekBarChangeListener {
    private static int seekvalue = 10;
    private static EditText edittext;


    public interface ToolbarListener {
        void onButtonClick(int pos, String tex);
    }

    ToolbarListener activityCallback;
    @Override
    public View onCreateView( LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.toolbarfragment, container, false);

        edittext= (EditText)view.findViewById(R.id.editTextTextPersonName);
        final Button button = (Button) view.findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonclicked(v);
            }
        });

        final SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekBar);

        seekBar.setOnSeekBarChangeListener(this);
        return view;

    }



    public void buttonclicked(View view) {

        activityCallback.onButtonClick(seekvalue, edittext.getText().toString());

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        seekvalue = progress;

    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar arg0) {

    }



}

片段二:

import ...

public class TextFragment extends Fragment {
    private static TextView textview;
    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {

        View view= inflater.inflate(R.layout.textfragment,container,false);

        textview = view.findViewById(R.id.textView);

        return view;
    }

    public void changetextproperties(int size, String text) {


        textview.setText(text);
        textview.setTextSize(size);
    }
}

主要Activity:


import ...

public class MainActivity extends FragmentActivity implements Toolbarfragment.ToolbarListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    @Override
    public void onButtonClick(int size, String text) {

        TextFragment textfragment= (TextFragment)getSupportFragmentManager().findFragmentById(R.id.fragment2);
        textfragment.changetextproperties(size, text);

    }
}

Logcat :

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.badatext, PID: 13829
    java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.badatext.Toolbarfragment$ToolbarListener.onButtonClick(int, java.lang.String)' on a null object reference
        at com.example.badatext.Toolbarfragment.buttonclicked(Toolbarfragment.java:52)
        at com.example.badatext.Toolbarfragment.onClick(Toolbarfragment.java:37)
        at android.view.View.performClick(View.java:6597)
        at android.view.View.performClickInternal(View.java:6574)
        at android.view.View.access00(View.java:778)
        at android.view.View$PerformClick.run(View.java:25885)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)




您可以使用 localbrodcastreceiver,或者您可以在启动片段时使用来自 intent 的额外值

想通了!

通过覆盖 Toolbarfragment 中的 OnAttach() 方法 class

@Override
    public void onAttach(Context context) {
        super.onAttach(context);

            activityCallback = (ToolbarListener) context;}