android 片段中显示alertdialog 的问题

发布于 2024-11-18 17:36:06 字数 804 浏览 1 评论 0原文

我遵循了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

注定孤独终老 2024-11-25 17:36:06

问题是因为您需要使用支持包的 FragmentManager,但当您调用 getFragmentManager() 时,您正在使用本机 FragmentManager。尝试在初始化变量 fm 时调用 getSupportFragmentManager()

The problem is because you need to be using the support package's FragmentManager but you are using the native FragmentManager when you call getFragmentManager(). Try calling getSupportFragmentManager() when initializing your variable fm.

2024-11-25 17:36:06

实际上,在按照 @Jacob 所说的操作之后,您还必须确保包含支持包中的 DialogFragment,而不是本机包中的 DialogFragment。

您可以通过导入来做到这一点,

import android.support.v4.app.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,

import android.support.v4.app.DialogFragment;
行至春深 2024-11-25 17:36:06

就我而言,我的 minSDK 设置为 14,因此我不想使用支持包。我的问题是我导入了错误的 DialogFragment ,如下所示:

import android.support.v4.app.DialogFragment;

我将其更改为这样并且它有效:

import android.app.DialogFragment;

In my case my minSDK is set to 14 so I did not want to use the support package. My problem was I was importing the wrong DialogFragment like so:

import android.support.v4.app.DialogFragment;

I changed it to this and it worked:

import android.app.DialogFragment;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文