无法覆盖 androidx.fragment.app.Dialog 的 onCreateDialog 方法

安德鲁史密斯289

当我尝试覆盖 的onCreateDialog方法时,androidx.fragment.app.DialogFragment出现以下错误:“onCreateDialog 没有覆盖任何内容”。

根据developer.android.com我应该能够覆盖它,因为它被定义为开放。

我正在使用以下代码

import android.app.AlertDialog
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import androidx.fragment.app.DialogFragment


class MyDialogFragment : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle): Dialog {
        return activity?.let {
            val builder = AlertDialog.Builder(it)
            builder.setMessage("Yes or no?")
                .setPositiveButton("yes",
                    DialogInterface.OnClickListener { dialog, id ->
                        // yes
                    })
                .setNegativeButton("no",
                    DialogInterface.OnClickListener { dialog, id ->
                        // no
                    })
            builder.create()
        } ?: throw IllegalStateException("Activity cannot be null")
    }
}

这里发生了什么?

强大的WOZ

如果您仔细查看方法签名,您将看到参数标有@Nullable,其类型为Bundle?

onCreateDialog(@Nullable savedInstanceState: Bundle?)

但是在您的情况下,您已将参数类型定义Bundle为不可为空,使您的函数签名与您尝试覆盖的函数签名不同,因此出现错误

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Fragment中重写onCreateDialog方法

Fragment $ Companion无法在BottomNavigationView侦听器上强制转换为androidx.fragment.app.Fragment

没有注释的onCreateDialog方法会覆盖以@NonNull注释的方法

无法启动活动ComponentInfo-膨胀类androidx.fragment.app.FragmentContainerView时出错

不可转换的类型;无法将“ androidx.fragment.app.Fragment”转换为“ com.google.android.gms.maps.MapFragment”

使用androidx.fragment.app.Fragment,我发现在initView()中无法初始化initAdapter(),为什么?

<androidx.fragment.app.FragmentContainerView> vs <fragment>作为NavHost的视图

将androidx.fragment.app.Fragment转换为android.app.fragment.Fragment吗?

Fragment $ InstantiationException关于从android.app.Activity膨胀androidx片段

'FragmentStatePagerAdapter(androidx.fragment.app.FragmentManager)' 被弃用

DatePickerDialog已涂黑androidx.fragment.app.DialogFragment中的按钮

使用androidx.fragment.app.FragmentContainerView时发生ClassNotFoundException

引起:java.lang.ClassCastException: androidx.fragment.app.FragmentContainerView 不能转换为 androidx.navigation.fragment.NavHostFragment

在androidx.fragment.app.Fragment中,不赞成使用setUserVisibleHint(),为什么不执行?

尝试使用NavHostFragment并收到错误消息:膨胀类androidx.fragment.app.FragmentContainerView时出错

空对象引用上的'boolean androidx.fragment.app.FragmentManagerImpl.isDestroyed()'

无法解析android.support.v4.app.Fragment(版本21.0.3)Android Studio中的方法getContext()

Android崩溃onCreate()-无法将片段强制转换为androidx.navigation.fragment.NavHostFragment

Dialog Fragment无法将事件传递回Android中调用Fragment?

击碎片段必须附加到@AndroidEntryPoint活动。找到:类androidx.fragment.app.testing.FragmentScenario $ EmptyFragmentActivity

无法覆盖preferredsHomeIndicatorAutoHidden()方法

无法覆盖toString方法

SupportMapFragment不支持AndroidX Fragment

无法解析Fragment类中的方法'getApplicationContext()'

无法解析Fragment中的方法“ getSupportFragmentManager()”

无法在Fragment RecyclerView中解析'findViewById'方法

我无法在Activity中调用方法Fragment

我无法从 Activity 调用 Fragment 方法

摆动错误:尝试调用虚拟方法'void androidx.appcompat.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)