将支持库更新为27.0.0后,片段中出现多个错误 [英] Multiple errors in my fragments after updating the support library to 27.0.0

查看:173
本文介绍了将支持库更新为27.0.0后,片段中出现多个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After updating the support library from v-26.1.0 to v-27.0.0 Multiple errors in my fragments.

以下是一些错误的列表:

here is a list of some these errors:

错误:无法智能地强制转换为捆绑",因为参数"是 这次可能已经更改的可变属性.

Error: Smart cast to 'Bundle' is impossible, because 'arguments' is a mutable property that could have been changed by this time.

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

Error: 'onCreateView' overrides nothing

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

Error: 'onViewCreated' overrides nothing

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

Error: Type mismatch: inferred type is View? but View was expected

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

Error: Type mismatch: inferred type is Context? but Context was expected

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

Error: Type mismatch: inferred type is FragmentActivity? but Context was expected

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

Error: Type mismatch: inferred type is FragmentActivity? but Context was expected

来自android studio模板中的空白片段.

from android studio's template for empty fragment.

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)
}

推荐答案

所有这些错误的根本原因是在支持库v-27.0.0中添加了@Nullable@NonNull注释.
而且由于kotlin语言了解可空性,并且NullableNonNull具有与Java不同的类型.
没有这些注释,编译器就无法区分它们,Android Studio正在竭尽全力来推断正确的类型.

The Root cause of all of these errors is that in support library v-27.0.0 @Nullable and @NonNullannotations have been added.
and since kotlin language is aware of nullability and has a different type for Nullable and NonNull, unlike Java.
without these annotations, the compiler has no way of differentiating between them, and Android studio was trying his best to infer the right type.

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

错误:无法智能地强制转换为捆绑",因为参数"是 这次可能已经更改的可变属性.

Error: Smart cast to 'Bundle' is impossible, because 'arguments' is a mutable property that could have been changed by this time.

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

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

Error: 'onCreateView' overrides nothing

频道:

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

==>

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


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

Error: 'onViewCreated' overrides nothing

更改:

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

==>

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


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

Error: Type mismatch: inferred type is Context? but Context was expected

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

if context is passed as argument to method, just use the quick fix to replace getContext() with getContext()?.let{}
the same applies to the kotlin short version context.

否则,如果用于调用某些方法,将getContext().someMethod()替换为getContext()?.someMethod()

else if is used to call some method replace getContext().someMethod() with getContext()?.someMethod()

kotlin短版context?.someMethod()也是如此.

the same applies to the kotlin short version context?.someMethod().

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

Error: Type mismatch: inferred type is FragmentActivity? but Context was expected

使用上一个错误的修复程序.

use the fix of the previous error.

这篇关于将支持库更新为27.0.0后,片段中出现多个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆