我正在尝试将信息从我的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();
}
这是我在片段代码中覆盖的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");
}
}
答案 0 :(得分:0)
解决方案::要将数据从警报对话框传递到父片段,更简单的方法是让对话框扩展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。
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();
}
}