繁体   English   中英

将参数从DialogFragment传递给Fragment

[英]Passing argument from a DialogFragment to Fragment

我遇到了一个问题,我想将SetDialogFragment回最初调用它的Fragment

我曾尝试实现一个接口,但似乎无法从Fragment使其正常工作。

还有其他方法可以从DialogFragment >> Fragment传递参数吗? 还是我需要在Activity上实现接口,然后从那里移动它?

这个问题似乎是NullPointerException ,我很确定这是因为该接口需要在Activity级别而不是在Fragment上实现。 当点击对话框的“正按钮”时,发生崩溃。

对话片段

public class CustomPermissionDialog extends DialogFragment implements
    OnCheckedChangeListener {

String _permission;
View convertView;
AlertDialog.Builder builder;
Switch alertDelete;
Set<String> permSet = new TreeSet<String>();

public static interface OnCompleteDialogInterface {
    public abstract void OnCompleteDialog(Set mPermSet);
}

private OnCompleteDialogInterface mInterface;

public CustomPermissionDialog(Context context, String permissionName) {

    _permission = permissionName;
    mInterface = (OnCompleteDialogInterface) getActivity();
    // TODO Auto-generated constructor stub
}

public Dialog onCreateDialog(Bundle savedInstanceState) {

    LayoutInflater inflater = getActivity().getLayoutInflater();
    builder = new AlertDialog.Builder(getActivity());

    if (_permission == "Alerts") {
        convertView = (View) inflater
                .inflate(
                        getResources().getLayout(
                                R.layout.alerts_perm_dialog), null);
        alertDelete = (Switch) convertView
                .findViewById(R.id.switchAlertDelete);
        alertDelete.setOnCheckedChangeListener(this);

    }
    if (_permission == "Automation") {
        convertView = (View) inflater.inflate(
                getResources().getLayout(R.layout.automation_perm_dialog),
                null);

    }
    if (_permission == "Books") {
        convertView = (View) inflater.inflate(
                getResources().getLayout(R.layout.books_perm_dialog), null);

    }

    if (_permission == "Codes") {
        convertView = (View) inflater.inflate(
                getResources().getLayout(R.layout.codes_perm_dialog), null);

    }

    if (_permission == "DBS") {
        convertView = (View) inflater.inflate(
                getResources().getLayout(R.layout.dbs_perm_dialog), null);

    }
    if (_permission == "Feedback") {
        convertView = (View) inflater.inflate(
                getResources().getLayout(R.layout.feedback_perm_dialog),
                null);

    }

    if (_permission == "Groups") {
        convertView = (View) inflater
                .inflate(
                        getResources().getLayout(
                                R.layout.groups_perm_dialog), null);

    }

    if (_permission == "Inventory") {
        convertView = (View) inflater.inflate(
                getResources().getLayout(R.layout.inventory_perm_dialog),
                null);

    }

    if (_permission == "Jobs") {
        convertView = (View) inflater.inflate(
                getResources().getLayout(R.layout.jobs_perm_dialog), null);

    }

    if (_permission == "Locations") {
        convertView = (View) inflater.inflate(
                getResources().getLayout(R.layout.locations_perm_dialog),
                null);

    }

    if (_permission == "Logs") {
        convertView = (View) inflater.inflate(
                getResources().getLayout(R.layout.logs_perm_dialog), null);

    }

    if (_permission == "Messages") {
        convertView = (View) inflater.inflate(
                getResources().getLayout(R.layout.messages_perm_dialog),
                null);

    }

    if (_permission == "Services") {
        convertView = (View) inflater.inflate(
                getResources().getLayout(R.layout.services_perm_dialog),
                null);

    }
    if (_permission == "Settings") {
        convertView = (View) inflater.inflate(
                getResources().getLayout(R.layout.settings_perm_dialog),
                null);

    }
    if (_permission == "Templates") {
        convertView = (View) inflater.inflate(
                getResources().getLayout(R.layout.templates_perm_dialog),
                null);

    }
    if (_permission == "Tools") {
        convertView = (View) inflater.inflate(
                getResources().getLayout(R.layout.tools_perm_dialog), null);

    }
    if (_permission == "Updates") {
        convertView = (View) inflater.inflate(
                getResources().getLayout(R.layout.updates_perm_dialog),
                null);

    }
    if (_permission == "Users") {
        convertView = (View) inflater.inflate(
                getResources().getLayout(R.layout.users_perm_dialog), null);

    }

    // defining the alertdialog
    builder.setTitle(_permission + " Permissions");

    builder.setView(convertView);
    builder.setPositiveButton(R.string.accept,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // do something with the new note
                    mInterface.OnCompleteDialog(permSet);

                }
            }).setNegativeButton(R.string.cancel,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });

    return builder.create();
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    // TODO Auto-generated method stub
    if (alertDelete.isChecked()) {
        // The toggle is enabled
        permSet.add("alert_delete");
        Log.e("ALERTDELETE", "CHECKED");
    } else {
        // The toggle is disabled
        permSet.remove("alert_delete");
        Log.e("ALERTDELETE", "UNCHECKED");
    }

}

}

片段内部

    @Override
public void OnCompleteDialog(Set mPermSet) {
    // TODO Auto-generated method stub
    this.permSet = mPermSet;
    String tempPermString = permSet.toString();
    Log.e("PERMISSIONS", tempPermString);

}

堆栈跟踪

    10-30 11:41:30.081: E/AndroidRuntime(16925): FATAL EXCEPTION: main
10-30 11:41:30.081: E/AndroidRuntime(16925): Process: com.e.main, PID: 16925
10-30 11:41:30.081: E/AndroidRuntime(16925): java.lang.NullPointerException
10-30 11:41:30.081: E/AndroidRuntime(16925):    at com.e.dialog.CustomPermissionDialog$1.onClick(CustomPermissionDialog.java:171)
10-30 11:41:30.081: E/AndroidRuntime(16925):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
10-30 11:41:30.081: E/AndroidRuntime(16925):    at android.os.Handler.dispatchMessage(Handler.java:102)
10-30 11:41:30.081: E/AndroidRuntime(16925):    at android.os.Looper.loop(Looper.java:136)
10-30 11:41:30.081: E/AndroidRuntime(16925):    at android.app.ActivityThread.main(ActivityThread.java:5105)
10-30 11:41:30.081: E/AndroidRuntime(16925):    at java.lang.reflect.Method.invokeNative(Native Method)
10-30 11:41:30.081: E/AndroidRuntime(16925):    at java.lang.reflect.Method.invoke(Method.java:515)
10-30 11:41:30.081: E/AndroidRuntime(16925):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
10-30 11:41:30.081: E/AndroidRuntime(16925):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)

空指针是由于尝试使用getActivity()DialogFragment的构造函数中设置mInterface引起的。 片段生命周期中的此时的getActivity()为null。

尝试在onCreate设置,或在调用活动时将其onCreate为接口(假设可以保证您的活动可以实现您的接口)

((OnCompleteDialogInterface) getActivity()).OnCompleteDialog(permSet);

另一个问题是您的字符串比较都是错误的。 您应该使用

if (_permission.equals("whatever"))

其次,您应该使用if / else语句检查_permission字符串,以避免不必要的检查。

此外,片段的应该具有空的构造函数。 您应该查看如何使用.setArgument(bundle)方法传递_permissions字符串作为片段

您的布局膨胀有点过于复杂

代替

 convertView = (View) inflater.inflate(getResources().getLayout(R.layout.jobs_perm_dialog), null);

您可以使用

 convertView = inflater.inflate(R.layout.jobs_perm_dialog, null);

您的对话框在构造函数中还没有活动,因此在其中调用getActivity()将返回null。 mInterface的初始化移动到onCreateDialog()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM