【问题标题】:Multiple errors in my fragments after updating the support library to 27.0.0将支持库更新到 27.0.0 后,我的片段中出现多个错误
【发布时间】:2021-08-30 03:17:34
【问题描述】:

将支持库从 v-26.1.0 更新到 v-27.0.0 后,我的片段中出现多个错误。

以下是其中一些错误的列表:

错误:无法智能转换为“Bundle”,因为“arguments”是 到这个时候可能已经更改的可变属性。

错误:'onCreateView' 不会覆盖任何内容

错误:'onViewCreated' 不会覆盖任何内容

错误:类型不匹配:推断类型是视图?但视图是 预计

错误:类型不匹配:推断类型是上下文?但是上下文 预料之中

错误:类型不匹配:推断类型是 FragmentActivity?但 上下文是预期的

错误:类型不匹配:推断类型是 FragmentActivity?但 上下文是预期的

来自 android studio 的空片段模板。

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    if (arguments != null) {
        mParam1 = arguments.getString(ARG_PARAM1)
        mParam2 = arguments.getString(ARG_PARAM2)
    }
}

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater!!.inflate(R.layout.fragment_blank, container, false)
}

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
}

【问题讨论】:

标签: android android-fragments kotlin android-support-library


【解决方案1】:

所有这些错误的根本原因是在支持库 v-27.0.0 中添加了 @Nullable@NonNullannotations。
并且由于 kotlin 语言知道可空性,并且与 Java 不同,NullableNonNull 具有不同的类型。
如果没有这些注解,编译器无法区分它们,Android Studio 正在尽最大努力推断出正确的类型。

TL;DR:更改类型以正确反映可空性状态。


错误:无法智能转换为“Bundle”,因为“arguments”是 到这个时候可能已经更改的可变属性。

更改arguments.getString(ARG_NAME) ==> arguments?.getString(ARG_NAME) ?: ""


错误:'onCreateView' 不会覆盖任何内容

陈:

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View?

==>

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?

错误:'onViewCreated' 不会覆盖任何内容

改变:

override fun onViewCreated(view: View?, savedInstanceState: Bundle?)

==>

override fun onViewCreated(view: View, savedInstanceState: Bundle?)

错误:类型不匹配:推断类型是上下文?但是上下文 预料之中

如果上下文作为参数传递给方法,只需使用快速修复将getContext()替换为getContext()?.let{}
这同样适用于 kotlin 短版 context

else if 用于调用某些方法将getContext().someMethod() 替换为getContext()?.someMethod()

同样适用于 kotlin 短版 context?.someMethod()


错误:类型不匹配:推断类型是 FragmentActivity?但 上下文是预期的

使用先前错误的修复。

【讨论】:

  • ?.let{} 添加到片段的活动中不允许我将其用作方法的参数。错误是Type mismatch: inferred type is FragmentActivity? but Context was expected。有其他人可以做到这一点吗?此外,例如,当我尝试访问cacheDir 时,我得到另一个:Smart cast to 'FragmentActivity' is impossible, because 'activity' is a property that has open or custom getter。还是得想办法解决这个问题。
  • @StéphanePéchard 这将解决您的问题。 stackoverflow.com/questions/41086296/…
  • @StéphanePéchard 使用 "requireContext()" 如果您在片段中获取非空上下文 - 如果上下文为空,它将抛出 IllegalStateException。
  • @humazed 我在大约 100 多个课程中遇到了这个问题,所以这不是一个大问题吗?
  • errrrrrrrrrr 这不仅仅是替换 1 或 2 个字符,而是大量替换它们!在某些情况下,它会影响代码流。最好降级到26。真的很烦人。我猜我使用 kotlin 和 android studio 确实犯了大错。
【解决方案2】:

对于错误信息:

Type mismatch: inferred type is Bundle? but Bundle was expected

课堂上:Fragment,方法:onCreateView

来自

val arguments = SleepQualityFragmentArgs.fromBundle(arguments)

val arguments = SleepQualityFragmentArgs.fromBundle(requireArguments())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2016-07-04
    相关资源
    最近更新 更多