0

'listener2.applyTexts2(infor);' 行 给我一个错误,我在 applyTexts2 上传递了一个空对象引用。我调试了这一行,'infor' 将所有值正常传递给它,但它仍然说它为空。我做错了什么?

(我从我的 MainActivity 中通过一个按钮调用它,插入信息的弹出窗口很好,一切看起来都很好,但是当我在数据中没有 null 时它说 null。我的 MainActivity 中有另一个侦听器,它工作正常.第一个会不会弄乱第二个?它们是完全独立的类)

public class StaffDialog extends AppCompatDialogFragment{

    private StaffDialogListener listener2;
    private EditText FirstName;
    private EditText LastName;
    private EditText Phone;
    private EditText Linkedin;
    private EditText Tran;
    private String[] infor = new String[5];

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.layout_dialog2,null);

        builder.setView(view)
                .setTitle("Enter this person's information")
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setPositiveButton("Done", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        infor[0]=FirstName.getText().toString();
                        infor[1]=LastName.getText().toString();
                        infor[2]=Phone.getText().toString();
                        infor[3]=Linkedin.getText().toString();
                        infor[4]=Tran.getText().toString();
                        listener2.applyTexts2(infor);
                    }
                });

        FirstName = view.findViewById(R.id.name);
        LastName = view.findViewById(R.id.lastname);
        Phone = view.findViewById(R.id.phone);
        Linkedin = view.findViewById(R.id.linkedin);
        Tran = view.findViewById(R.id.tran);
        return builder.create();
    }

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

        try{
            listener2 = (StaffDialogListener) context;
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public interface StaffDialogListener{
        void applyTexts2(String[] info);
    }
}```
4

0 回答 0