java onCreateDialog 上的“覆盖已弃用的方法”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32239917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:52:37  来源:igfitidea点击:

"Overrides deprecated method" on onCreateDialog

javaandroid

提问by Francisco Romero

I have a class in which I'm trying to create a Dialogbut when I put the method onCreateDialogit appears crossed by a line. It gives to me the following error message:

我有一个类,我试图在其中创建一个,Dialog但是当我放置该方法时,onCreateDialog它似乎被一条线交叉了。它给了我以下错误消息:

Overrides deprecated method in 'android.app.Activity'.

This inspection reports where deprecated method is used in the specified inspection scope.

覆盖“android.app.Activity”中已弃用的方法。

本检验报告在指定检验范围内使用不赞成使用的方法。

But when I go to the official documentation of Android I didn't see that this method it's deprecated so I don't understand what happens: onCreateDialog

但是当我查看 Android 的官方文档时,我没有看到这种方法已被弃用,所以我不明白会发生什么:onCreateDialog

My onCreateDialogcode:

我的onCreateDialog代码:

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case 1:
            return new DatePickerDialog(this, datePickerListener, year, month, day);
        case 2:
            return new TimePickerDialog(this, timePickerListener, hour, minute,false);
    }

    return null;
}

But if I execute my application, it works properly so I have some questions:

但是如果我执行我的应用程序,它可以正常工作,所以我有一些问题

  • Why it let to me execute the application?

  • How can I solve it?

  • Is it bad that this message appears? I mean, could be a problem associated by it? Could it generate future problems?

  • 为什么它让我执行应用程序?

  • 我该如何解决?

  • 出现此消息是不是很糟糕?我的意思是,可能是与它相关的问题吗?它会产生未来的问题吗?

Any help that helps me to clarify would be really appreciated.

任何帮助我澄清的帮助将不胜感激。

Thanks in advance!

提前致谢!

回答by Karakuri

Activity.onCreateDialog()is deprecated.

Activity.onCreateDialog()已弃用。

This method was deprecated in API level 13. Use the new DialogFragment class with FragmentManager instead; this is also available on older platforms through the Android compatibility package.

此方法在 API 级别 13 中已弃用。请改用带有 FragmentManager 的新 DialogFragment 类;这也可以通过 Android 兼容包在旧平台上使用。

Deprecated code means it's use is discouraged and may not be supported going forward. That doesn't necessarily mean the code doesn't work; it may still run as before, but if changes are made that break this functionality, there's no guarantee anyone will fix it since they've already stated it should not be used.

弃用的代码意味着不鼓励使用它,并且可能不支持它。这并不一定意味着代码不起作用;它可能仍像以前一样运行,但如果进行了破坏此功能的更改,则不能保证有人会修复它,因为他们已经声明不应使用它。

To "fix" it, you should look at DialogFragmentand how to use fragments in general, and show one of those instead.

要“修复”它,您应该查看DialogFragment一般情况下如何使用片段,并显示其中之一。