Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将信息从AlertDialog传递到父片段_Java_Android_Parameter Passing_Android Alertdialog - Fatal编程技术网

Java 将信息从AlertDialog传递到父片段

Java 将信息从AlertDialog传递到父片段,java,android,parameter-passing,android-alertdialog,Java,Android,Parameter Passing,Android Alertdialog,我试图将信息从AlertDialog传递到它所在的父片段。但只要你点击“确定”按钮,应用程序就会崩溃。 我真的不知道该做什么了,我已经读了很多帖子和文章,但没有发现问题。 如果你能帮我,那就太好了。(我是初学者) 下面是我在代码中遇到的第一个问题和第二个注释的错误 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.test, PID: 20682 java.lang.NullPointerException:

我试图将信息从AlertDialog传递到它所在的父片段。但只要你点击“确定”按钮,应用程序就会崩溃。 我真的不知道该做什么了,我已经读了很多帖子和文章,但没有发现问题。 如果你能帮我,那就太好了。(我是初学者)

下面是我在代码中遇到的第一个问题和第二个注释的错误

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 void onAttach(Context context) {
        super.onAttach(context);

        try {
            listener = (SchulfachDialogListener) context;
        } catch (ClassCastException e) {
   //        throw new ClassCastException(context.toString()+
  //                 "must implement SchulfachDialogListener");  
        }
    }
这是我在片段代码中覆盖的applyTexts:

public interface SchulfachDialogListener{
        void applyTexts(String name);

    }
}


@Override public void applyTexts(String name) {
            test = name;
    }

}
我还有一个块,其中2条注释行在单击启动警报对话框的按钮时导致崩溃:

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 void onAttach(Context context) {
        super.onAttach(context);

        try {
            listener = (SchulfachDialogListener) context;
        } catch (ClassCastException e) {
   //        throw new ClassCastException(context.toString()+
  //                 "must implement SchulfachDialogListener");  
        }
    }

解决方案:要将数据从警报对话框传递到父片段,更简单的方法是让对话框扩展DialogFragment。然后使用
setTargetFragment
setTargetFragment
在它们之间发送数据

因为您不发布父片段代码,所以我假设这里是父片段的xml和java代码

MainFragment.java

public class MainFragment extends Fragment implements SchulfachDialogListener{

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);

        Button btnShowAlertDialog = view.findViewById(R.id.button_show_alert_dialog);
        btnShowAlertDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SchulfachDialog dialog = new SchulfachDialog();
                dialog.setTargetFragment(MainFragment.this, 0);
                dialog.show(requireActivity().getSupportFragmentManager(), null);
            }
        });

        return view;
    }

    @Override
    public void applyTexts(String text) {
        Toast.makeText(requireActivity(), text, Toast.LENGTH_SHORT).show();
    }
}
fragment_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

    <Button android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:text="Show Alert Dialog"
        android:id="@+id/button_show_alert_dialog"/>

</LinearLayout>

解决方案:要将数据从警报对话框传递到父片段,更简单的方法是让对话框扩展DialogFragment。然后使用
setTargetFragment
setTargetFragment
在它们之间发送数据

因为您不发布父片段代码,所以我假设这里是父片段的xml和java代码

MainFragment.java

public class MainFragment extends Fragment implements SchulfachDialogListener{

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);

        Button btnShowAlertDialog = view.findViewById(R.id.button_show_alert_dialog);
        btnShowAlertDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SchulfachDialog dialog = new SchulfachDialog();
                dialog.setTargetFragment(MainFragment.this, 0);
                dialog.show(requireActivity().getSupportFragmentManager(), null);
            }
        });

        return view;
    }

    @Override
    public void applyTexts(String text) {
        Toast.makeText(requireActivity(), text, Toast.LENGTH_SHORT).show();
    }
}
fragment_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

    <Button android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:text="Show Alert Dialog"
        android:id="@+id/button_show_alert_dialog"/>

</LinearLayout>
listener.applyTexts(名称);这里的侦听器为空。共享您设置listenerlistener.applyTexts(名称)的源代码(父片段);这里的侦听器为空。共享您设置侦听器的位置(父片段)的代码