android - 为什么 Kotlin 的 mutableListOf() 返回空值

标签 android kotlin

我正在通过 Google 的 Android 开发人员代码实验室为 unscramble 应用程序工作。我正在尝试初始化我的变量 wordsList是一个空的可变字符串列表:

private var wordsList: MutableList<String> = mutableListOf()

但是,在调试过程中,我看到 wordsList 的值是null而不是像 [] 这样的空列表.我在 kotlin playground 测试了代码,它应该是 [] .我附上了 android studio 向我展示的截图。我做错了什么,为什么我没有看到预期的行为? enter image description here

这是我的项目的 gradle 文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = "1.4.21"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

和我的应用程序的 gradle 文件


plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.example.android.unscramble"
        minSdkVersion 23
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding = true
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    // LiveData
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
    // ViewModel
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
    implementation 'androidx.fragment:fragment-ktx:1.2.5'
}

最佳答案

引用Kotlin docs :

During an instance initialization, the initializer blocks are executed in the same order as they appear in the class body, interleaved with the property initializers

因此,当 wordsList 尚未初始化时,您的断点可能是从上方的 init { } block 命中的。您可以选择在调用 getNextWord() 之前初始化您的变量,或者只是将此 init { } block 向下移动,以便在您的所有属性都已初始化时执行它。

关于android - 为什么 Kotlin 的 mutableListOf() 返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67816065/

相关文章:

android - Dagger 2,在模块中提供应用上下文

使用可为 nullable parcelable 参数的编译中的 Android 数据绑定(bind)错误

java - 从 EditText 字段获取数据

ProGuard 的 AndroidX 构建问题

android - 如何在不使用 android_app* 的情况下获取 ANativeActivity 指针 un Cpp

arrays - 如何在 Kotlin 中将没有空格的字符串拆分为整数数组?

types - 为什么 Kotlin 不支持无符号整数?

java - 2D 游戏,随机创建的对象

android - 如何在没有 View 的情况下创建计时器?

android - 如何在 kotlin 中将函数作为参数传递 - Android