【问题标题】:unresolved reference bundle android studio未解决的参考包 android studio
【发布时间】:2020-01-05 21:00:03
【问题描述】:

我刚开始为 android studio 学习 kotlin 并构建了我的第一个项目,除了 android studio 给我这个错误之外,我什么都没碰: 未解析的参考包,即使尝试导入 android.os.Bundle 也没有用。 起初我得到了错误: 未解决的参考 R,但是当我更新 kotlin 插件时,得到了我已经告诉过你的先前错误。 正如我所说,我从未接触过任何东西,这些文件是由 android studio 本身构建的。 有什么想法吗?

这是应用级别的 gradle:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.deathstroke.samplekotlinproject"
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner 
"android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
         release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 
'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

这是项目级别的 gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.3.31'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.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
}

这是主要的活动类:

package com.example.deathstroke.samplekotlinproject

import android.support.v7.app.AppCompatActivity
import android.os.bundle;

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: android.os.Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

android studio 向我展示了红色的 Bunlde,而 alt + Enter 没有作为导入类的选项...

【问题讨论】:

    标签: android gradle kotlin android-gradle-plugin


    【解决方案1】:

    Kotlin 中的类名区分大小写。所以你必须导入Bundle 而不是bundle

    import android.os.Bundle
    

    【讨论】:

    • android.os 建议列表中没有名为 Bundle 的类。因为如果有的话,Alt + Enter 会为我导入。
    • 这很奇怪,因为Android框架肯定有这个类。也许您必须使 AS 缓存无效。你可以通过File > Invalidate Caches / Restart > Invalidate and Restart
    • 已经尝试过了,但也没有用,所以我不得不将 android studio 更新到 3.5.0 版本,将 Kotlin 更新到 1.3.50,现在一切都很好,需要注意。
    【解决方案2】:

    你在 java 代码中导入,但你在 kotlin 中导入方法

    删除你的导入

      import android.os.bundle;
    

    然后替换

    override fun onCreate(savedInstanceState: Bundle?) 
    

    Alt+Enter 导入将自动包含在 android studio 中。

    import android.os.Bundle 
    

    然后清理并重建项目

    【讨论】:

      猜你喜欢
      • 2021-11-05
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 2021-03-28
      • 2019-01-08
      • 1970-01-01
      相关资源
      最近更新 更多