android 片段中显示alertdialog 的问题
我遵循了 android 示例,但出现了一个不全面的错误:
void showDialog() {
DialogFragment newFragment = MyAlertDialogFragment.newInstance();
newFragment.show(fm, "alert");
}
public static class MyAlertDialogFragment extends DialogFragment {
public static MyAlertDialogFragment newInstance() {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
return frag;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity()).setTitle("test")
.setMessage("bla bla bla").create();
}
}
newFragment.show(fm, "alert");
返回一个错误:
DialogFragment 类型中的方法 show(FragmentManager, String) 是 不适用于参数(FragmentManager、String)
有人可以帮助我吗?
I have followed android example but I have an incomprehensive error :
void showDialog() {
DialogFragment newFragment = MyAlertDialogFragment.newInstance();
newFragment.show(fm, "alert");
}
public static class MyAlertDialogFragment extends DialogFragment {
public static MyAlertDialogFragment newInstance() {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
return frag;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity()).setTitle("test")
.setMessage("bla bla bla").create();
}
}
newFragment.show(fm, "alert");
returns me an error :
The method show(FragmentManager, String) in the type DialogFragment is
not applicable for the arguments (FragmentManager, String)
Someone could help me ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是因为您需要使用支持包的
FragmentManager
,但当您调用getFragmentManager()
时,您正在使用本机FragmentManager
。尝试在初始化变量fm
时调用getSupportFragmentManager()
。The problem is because you need to be using the support package's
FragmentManager
but you are using the nativeFragmentManager
when you callgetFragmentManager()
. Try callinggetSupportFragmentManager()
when initializing your variablefm
.实际上,在按照 @Jacob 所说的操作之后,您还必须确保包含支持包中的 DialogFragment,而不是本机包中的 DialogFragment。
您可以通过导入来做到这一点,
Actually after you do as @Jacob says, you also have to make sure that you include DialogFragment from the Support package and not from the native package.
You can do that by importing,
就我而言,我的
minSDK
设置为14
,因此我不想使用支持包。我的问题是我导入了错误的DialogFragment
,如下所示:我将其更改为这样并且它有效:
In my case my
minSDK
is set to14
so I did not want to use the support package. My problem was I was importing the wrongDialogFragment
like so:I changed it to this and it worked: