java - 将参数从 DialogFragment 传递到 Fragment

标签 java android android-fragments

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

我尝试实现一个接口(interface),但我似乎无法让它从 fragment 正常工作。

是否有另一种方式从 DialogFragment >> Fragment 获取参数?或者我是否需要在 Activity 上实现接口(interface),然后将其从那里移动?

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

对话框 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");
    }

}

}

fragment 内部

    @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 造成的。此时 fragment 生命周期中的 getActivity() 为 null。

尝试在onCreate中设置它,或者在调用它时将 Activity 强制转换为接口(interface)(假设您的 Activity 保证实现您的接口(interface))

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

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

if (_permission.equals("whatever"))

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

此外, fragment 应该有空的构造函数。您应该了解如何使用 fragment 的 .setArgument(bundle) 方法传递 _permissions 字符串

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

而不是

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

你可以直接使用

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

关于java - 将参数从 DialogFragment 传递到 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26657427/

相关文章:

java - 我们为什么需要 ArrayList 构造函数中的 super() 呢?

java - 在 siteadmin 中创建页面并单击后,如何使该组件立即显示在 Sidekick 中?

android - 将图库图像设置为按钮背景

android - FileWriter 不在 Android 中写入文件?

java.lang.IllegalStateException : Two different ViewHolders have the same stable ID

java - 写入具有 Vector 属性的对象时出现 java.io.NotSerializedException

java - 访问同一个包中的私有(private)内部类

android - 从 R.style 创建 AttributeSet

android - 在软键盘上回击后,setError 弹出窗口没有正确重新定位?

android - 动态替换 fragment 而不是在按钮单击或方向更改时