将信息从警报对话框传递到父片段

我正在尝试将信息从我的警报对话框传递到它所在的父片段。但是,一旦您按下正向按钮,该应用程序就会崩溃。我真的不知道该怎么办已经阅读了许多帖子和文章,但找不到问题。如果你能帮助我,那就太好了。(我是初学者)


这是我在代码中的第一个问题和第二个注释中得到的错误。


E/AndroidRuntime: FATAL EXCEPTION: main

    Process: com.example.test, PID: 20682

    java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.test.SchulfachDialog$SchulfachDialogListener.applyTexts(java.lang.String)' on a null object reference

        at com.example.test.SchulfachDialog$1.onClick(SchulfachDialog.java:39)

        at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)

        at android.os.Handler.dispatchMessage(Handler.java:102)

        at android.os.Looper.loop(Looper.java:148)

        at android.app.ActivityThread.main(ActivityThread.java:5525)

        at java.lang.reflect.Method.invoke(Native Method)

        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)

        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

这是我的警报对话框代码:


builder.setView(view)

            .setTitle("Add new subject")

            .setMessage("Message")

            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override

                public void onClick(DialogInterface dialog, int which) {


                }

            })

            .setPositiveButton("ok", new DialogInterface.OnClickListener() {

                @Override

                public void onClick(DialogInterface dialog, int which) {

                    String name = editTextName.getText().toString();

                    listener.applyTexts(name); // Problem 1: when positiv Button is pushed this line causes a crash

                }

            });


    editTextName = view.findViewById(R.id.edit_name);


    return builder.create();

}

这是我在片段代码中覆盖的应用文本:


public interface SchulfachDialogListener{

        void applyTexts(String name);


    }

}



@Override public void applyTexts(String name) {

            test = name;

    }


}


紫衣仙女
浏览 67回答 1
1回答

烙印99

溶液:要将数据从警报对话框传递到父片段,更简单的方法是让对话框扩展 DialogFragment。然后使用它们和在它们之间发送数据。setTargetFragmentsetTargetFragment因为你没有发布父片段代码,所以我假设这里是你的父片段的xml和java代码。主碎屑.javapublic class MainFragment extends Fragment implements SchulfachDialogListener{&nbsp; &nbsp; @Nullable&nbsp; &nbsp; @Override&nbsp; &nbsp; public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; View view = inflater.inflate(R.layout.fragment_main, container, false);&nbsp; &nbsp; &nbsp; &nbsp; Button btnShowAlertDialog = view.findViewById(R.id.button_show_alert_dialog);&nbsp; &nbsp; &nbsp; &nbsp; btnShowAlertDialog.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SchulfachDialog dialog = new SchulfachDialog();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dialog.setTargetFragment(MainFragment.this, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dialog.show(requireActivity().getSupportFragmentManager(), null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; return view;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void applyTexts(String text) {&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(requireActivity(), text, Toast.LENGTH_SHORT).show();&nbsp; &nbsp; }}fragment_main.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:orientation="vertical">&nbsp; &nbsp; <Button android:layout_width="match_parent"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:text="Show Alert Dialog"&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/button_show_alert_dialog"/></LinearLayout>让警报对话框扩展 DialogFragment。public class SchulfachDialog extends DialogFragment {&nbsp; &nbsp; private EditText editTextName;&nbsp; &nbsp; @NonNull&nbsp; &nbsp; @Override&nbsp; &nbsp; public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());&nbsp; &nbsp; &nbsp; &nbsp; View view = LayoutInflater.from(requireActivity()).inflate(R.layout.dialog_schulfach, null);&nbsp; &nbsp; &nbsp; &nbsp; builder.setView(view)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setTitle("Add new subject")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setMessage("Message")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(DialogInterface dialog, int which) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setPositiveButton("ok", new DialogInterface.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(DialogInterface dialog, int which) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String name = editTextName.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SchulfachDialogListener listener = (SchulfachDialogListener) getTargetFragment();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listener.applyTexts(name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; editTextName = view.findViewById(R.id.edit_name);&nbsp; &nbsp; &nbsp; &nbsp; return builder.create();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java