gpt4 book ai didi

android - fragment 应该是静态的,这样它们可以被系统重新实例化,而匿名类不是静态的

转载 作者:太空狗 更新时间:2023-10-29 16:30:17 27 4
gpt4 key购买 nike

以下代码显示了以下错误:

“fragment 应该是静态的,以便它们可以被系统重新实例化,而匿名类不是静态的”

我该如何解决?

public void A(){
final DialogFragment dialogFragment =
new DialogFragment() {

@Override
public Dialog onCreateDialog(Bundle bundle) {
AlertDialog.Builder builder =
new AlertDialog.Builder(getActivity());

builder.setMessage("Hello");
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
B();
}
}
);

return builder.create();
}
};
}

private void B() {
//...
}

最佳答案

读取 fragment 生命周期你应该这样使用

public static class MyAlertDialogFragment extends DialogFragment {

public static MyAlertDialogFragment newInstance(int title) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");

return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(title)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doPositiveClick();
}
}
)
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doNegativeClick();
}
}
)
.create();
}
}

关于android - fragment 应该是静态的,这样它们可以被系统重新实例化,而匿名类不是静态的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41260604/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com