简体   繁体   English

方法无法识别 kotlin 中的变量

[英]Method doesn't recognize my variable in kotlin

Something really strange is happening.真正奇怪的事情正在发生。

If I execute fetchListOfNews() I get an error.如果我执行 fetchListOfNews() 我得到一个错误。

class SearchNewsViewModel(repository: Repository) : NewsViewModel(repository) {

    var keyword: String = "word"

    override suspend fun fetchListOfNews(): List<News> = repository.getNews(keyword)

    class Factory(private val repository: Repository) : ViewModelProvider.Factory {
        @Suppress("UNCHECKED_CAST")
        override fun <T : ViewModel> create(modelClass: Class<T>): T =
            SearchNewsViewModel(repository) as T
    }
}
override suspend fun getNews(keyword: String): List<News> {
        return listOfNews.filter { news -> news.title.contains(keyword) }
    }

在此处输入图像描述

But when I execute this code I don't get any error:但是当我执行这段代码时,我没有收到任何错误:

class SearchNewsViewModel(repository: Repository) : NewsViewModel(repository) {

    var keyword: String = "word"

    override suspend fun fetchListOfNews(): List<News> = repository.getNews("word")

    class Factory(private val repository: Repository) : ViewModelProvider.Factory {
        @Suppress("UNCHECKED_CAST")
        override fun <T : ViewModel> create(modelClass: Class<T>): T =
            SearchNewsViewModel(repository) as T
    }
}

I only changed the variable keyword to its value.我只是将变量关键字更改为它的值。

Why does it happen?为什么会这样? How can I solve it?我该如何解决?

I found the solution.我找到了解决方案。 I call fetchListOfNews() in the init block of NewsViewModel.我在 NewsViewModel 的 init 块中调用 fetchListOfNews()。 The init block is executed before initializing keyword. init 块在初始化关键字之前执行。

In the generated Java bytecode for your first snippet, keyword is not initialized until the constructor runs:在为您的第一个片段生成的 Java 字节码中,在构造函数运行之前不会初始化keyword

@NotNull
private String keyword;

public SearchNewsViewModel(@NotNull Repository repository) {
    Intrinsics.checkParameterIsNotNull(repository, "repository");
    super();
    this.repository = repository;
    this.keyword = "word";
}

@Nullable
public Object fetchListOfNews(@NotNull Continuation $completion) {
    return this.repository.getNews(this.keyword, $completion);
}

In the second snippet, everything is the same except that fetchListOfNews() no longer references keyword :在第二个片段中,除了fetchListOfNews()不再引用keyword之外,一切都相同:

@Nullable
public Object fetchListOfNews(@NotNull Continuation $completion) {
    return this.repository.getNews("word", $completion);
}

So, if your first implementation's fetchListOfNews() is called without the constructor for the class having finished execution, then you would hit this NPE.因此,如果在没有完成执行 class 的构造函数的情况下调用您的第一个实现的fetchListOfNews() ,那么您将遇到此 NPE。

How is your class being instantiated?您的 class 是如何被实例化的? While not common, there are ways to create instances without calling the constructor.虽然不常见,但有一些方法可以在不调用构造函数的情况下创建实例。

Or perhaps fetchListOfNews() is called in the super() constructor?或者也许fetchListOfNews()super()构造函数中被调用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM