- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到了一个问题,我想将 Set
从 DialogFragment
传递回最初调用它的 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/
我有一个 AppCompatActivity,它在某个时候显示一个 DialogFragment。在此对话框中,有些项目在删除之前需要确认。通过另一个 Yes/No DialogFragment 询问
我们添加一个通用的/普通的 fragment ,以编程的方式做一些事情,比如: fragmentTransaction.add(containerViewId, fragmentToAdd, frag
我有一个 DialogFragment 向用户显示选项列表,其中一个选项是“删除”选项,当用户按下删除选项时我想显示另一个 DialogFragment 作为确认,不幸的是,确认对话框没有显示。 这是
假设我在一个名为 my_dialog_fragment.xml 的 xml 布局文件中指定了 DialogFragment 的布局,并指定了 layout_width 和 >layout_height
当我设置对话框 fragment 的样式时,android 允许截取对话框 fragment 的屏幕截图,但在删除样式后它会起作用。 下面是对话框 fragment 的onCreate代码 @Over
我的应用程序中有一个带有日期和时间字段的 DialogFragment1(由 MyFragmentActivity 调用)。当用户单击它们时,会出现 DatePickerFragment 和 Time
显示 ProgressDialog 时应用程序崩溃。我正在使用滑出式键盘在手机上进行测试。显示对话框时,我滑动键盘,应用崩溃。 public static class ProgressDialogFr
正如问题中提到的.. 我有一个 fragment ,我从中创建并显示一个 DialogFragment,并将 targetFragment 设置为此当前 fragment 。 DialogFragme
如何在此 DialogFragment 中检索 editText1?它存在于 vraag_banen.xml 中,但 getView() 为空。 该对话框显示正常,也没有编译错误,但是我无法弄清楚如何
我应该如何创造这个设计的优越部分? 我正在使用 DialogFragment 在弹出窗口中执行此操作,但无法实现顶部透明的效果。 按钮设计但 background_circle: 使用
最后,我发现问题是什么...... 如果你有这样的功能: public void test() { DialogFragment dlg = new LoginDialog(); dl
我有一个带有监听器的 DialogFragment,用于单击按钮以调用 fragment 中的函数。 我收到 lateinit property listener has not been initi
我在 Activity 上有一个 DialogFragment。此 DialogFragment 是一个由 Web 服务调用填充的 ListView 。单击此 ListView 中的一行后,对话框应消
我正在尝试遵循本教程:http://developer.android.com/guide/topics/ui/controls/pickers.html#DatePicker 这是他们提供的代码,所
有什么方法可以更改 DialogFragment 中正面、负面和中性按钮的显示顺序,以便我可以先放置负面按钮? 我不想改变按钮的“性质”,而是保持它们为“积极”、“消极”和“中性”。 最佳答案 尝试这
这里可能重复,抱歉... 我不知道如何将自定义样式添加到我的 DialogFragment。此刻我正在上课 public final class SelectFragment extends Dial
我花了几个小时来让这个简单的用例正常工作:当我按下音量硬件按钮时,我想打开一个 DialogFragment。该对话框包含一个 SeekBar,应根据按下的按钮进行设置。 我这里有几个问题。 首先,我
DialogFramgment 有问题(在多个 Android 4.2/4.4 设备上支持 v4 库)。 我有两个 DialogFragment:EditAccountDialogFragment 和
当我将有效代码从 fragment 复制到dialogfragment时,它突然不起作用。 在常规 fragment 上:将位图保存在 sd 卡上。 on dialogfragment: 不将位图保存
我花了一整天的时间试图弥补这一点,但我不能.. 这就是问题所在:我想要一个是/否 AlertDialog方向改变时不会消失,所以我决定使用 DialogFragment . 所以我准备了代码并第一次使
我是一名优秀的程序员,十分优秀!