我试图将我的AlertDialog中的信息传递给它所在的父片段。但只要你点击肯定按钮,应用程序就会崩溃。我真的不知道什么todo已经阅读了很多帖子和文章,但找不到问题。如果你可以帮助我,那会很棒。(我是初学者)
这是我在Code中遇到的第一个问题和第二个评论的错误。
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();
}
这是我在片段代码中重写的applyTexts:
public interface SchulfachDialogListener{
void applyTexts(String name);
}
}
@Override public void applyTexts(String name) {
test = name;
}
}
我也有这个块,当点击启动警报对话框的按钮时,2条注释行会导致崩溃:
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代码。
main fragment.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。
public class SchulfachDialog extends DialogFragment {
private EditText editTextName;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
View view = LayoutInflater.from(requireActivity()).inflate(R.layout.dialog_schulfach, null);
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();
SchulfachDialogListener listener = (SchulfachDialogListener) getTargetFragment();
listener.applyTexts(name);
}
});
editTextName = view.findViewById(R.id.edit_name);
return builder.create();
}
}