DialogFragment使用接口回调到Target Fragment

时间:2015-06-19 06:47:24

标签: android android-fragments interface callback android-dialogfragment

我正在尝试将一些数据从dialogfragment发送到目标fragment,但它无效。

我编写了以下代码,但是通过exception

java.lang.NullPointerException: Attempt to invoke interface method 'void com.x.x.FragmentAlertDialog$Communicator.setI(java.lang.String)' on a null object reference

代码:

public class FragmentAlertDialog extends DialogFragment {

Communicator callback;

public interface Communicator {
    void setI(String name);
}

public static FragmentAlertDialog newInstance(String id, int title, int items) {
    FragmentAlertDialog frag = new FragmentAlertDialog();
    Bundle args = new Bundle();
    args.putString("id", id);
    args.putInt("title", title);
    args.putInt("items", items);
    frag.setArguments(args);
    return frag;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    callback = (Communicator) getTargetFragment();
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    String id = getArguments().getString("id");
    int title = getArguments().getInt("title");
    String[] items = getResources().getStringArray(getArguments().getInt("items"));
    switch (id) {
        case MainActivity.DIALOG_1:
            return new AlertDialog.Builder(getActivity())
                    .setTitle(title)
                    .setItems(items, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            callback.setI("Important Data");
                        }
                    }).create();
    }
    return null;
}

片段类

public class FragmentXYXY extends Fragment implements View.OnClickListener, FragmentAlertDialog.Communicator {

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.et_i:
            DialogFragment newFragment = FragmentAlertDialog.newInstance(MainActivity.DIALOG_I, R.string.i_select, R.array.i_array);
            newFragment.setTargetFragment(this, 0);
            newFragment.show(getFragmentManager(), "dialog");
    }
}

@Override
public void setI(String name) {
    textI.setText(name);
}

任何人都可以帮忙。 谢谢

Code Modified- newFragment.setTargetFragment(this,0);

2 个答案:

答案 0 :(得分:7)

你必须致电setTargetFragment

setTargetFragment(this, 0);

以便在FragmentXYXY

中获得参考DialogFragment
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.et_i:
            DialogFragment newFragment = FragmentAlertDialog.newInstance(MainActivity.DIALOG_I, R.string.i_select, R.array.i_array);
            newFragment.setTargetFragment(this, 0);
            newFragment.show(getFragmentManager(), "dialog");
    }
}

答案 1 :(得分:3)

例如,您可以这样做:

public interface DialogClickListener {
    public void onYesClick();
    public void onNoClick();
}


public class MyListFragment extends ListFragment implements DialogClickListener {

    ...

    private void showDialog() {
        DialogFragment dialog = new MyDialogFragment();
        dialog.setTargetFragment(this, 0);
        dialog.show(getFragmentManager(), "dialog");
    }

    @Override
    public void onYesClick() {
        // do something
    }

    @Override
    public void onNoClick() {
        // do something
    }
}


public class MyDialogFragment extends DialogFragment {
    private DialogClickListener callback;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            callback = (DialogClickListener) getTargetFragment();
        } catch (ClassCastException e) {
            throw new ClassCastException("Calling fragment must implement DialogClickListener interface");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("message")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        callback.onYesClick();
                    }
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        callback.onNoClick();
                    }
                });

        return builder.create();
    }
}

有关详细信息,请参阅此link