在AndroidStudio项目中看不到androidxcore-ktx扩展

看来我的 Android Studio 无法识别'androidx.core:core-ktx:1.2.0'包。Android Studio 版本:3.6.1

看来我的 Android Studio 无法识别'androidx.core:core-ktx:1.2.0'包。Android Studio 版本:3.6.1

Mygradle.properties:
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official

我的应用模块build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
   compileSdkVersion 29
   defaultConfig {
       applicationId "cz.nn.example.myapplication"
       minSdkVersion 25
       targetSdkVersion 29
       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.toString()
    targetCompatibility = JavaVersion.VERSION_1_8.toString()
  }
  kotlinOptions {
    jvmTarget = "1.8"
  }
}
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
}

当我尝试使用扩展功能,如:

cl MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val manager = systemService<NotificationManager>()
        SharedPreferences.edit {
        }
    }
}

函数systemService<NotificationManager>()edit无法识别。我做错了什么?

1

TL;博士

你用错了

首先SharedPreferences.edit()扩展函数不是静态的,需要使用SharedPreferences的实例。试试这个:

val prefs = getSharedPreferences("com.example.yourapp", Context.MODE_PRIVATE)
prefs.edit { putString("myString", "myValue") }

其次,该方法被称为getSystemService,而不是systemService

val manager = getSystemService<NotificationManager>()

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(232)
在一段时间内可能发生多少次 I/O中断
上一篇
使用 LWRP的厨师食谱名称中允许的字符
下一篇

相关推荐

  • android折线图控件//github.com/zhaojun1998/markdown_photos/blob/master

    Android折线图控件是一种可以将数据可视化的控件,可以用来展示数据之间的关系。它可以帮助我们更好地理解数据,并且可以通过折线图更直观地展示出数据之间的趋势。…

    2023-04-15 11:23:33
    0 68 16
  • android service 通信:实现 Android Service 之间的通信

    Android Service 通信是指在 Android 应用程序中,Service 和 Activity 之间的通信。Android Service 是一种特殊的组件,它可以在后台运行,不需要用户交互就能够完成其工作。这样的服务可以在后台播放音乐、下载数据、定时执行任务等等。…

    2023-05-03 03:32:44
    0 40 44
  • android 回调函数:使用onActivityResult()实现Android应用间的交互

    Android 回调函数是指在程序中,一个函数会将另一个函数作为参数,并在特定的事件发生时调用该函数。回调函数可以把复杂的操作封装成一个函数,然后在需要的时候调用,这样可以简化程序的编写。…

    2023-05-24 04:28:28
    0 49 60
  • android 静态变量Context.MODE_PRIVATE使用Context.MODE_PRIVATE实现Android应

    示例示例Android 静态变量是指在类中定义的静态变量,它的值不会随着对象的变化而变化,而是在类加载时就已经初始化好了。代码示例:…

    2023-02-06 01:30:20
    0 39 26
  • android svg使用:Using SVG in Android for Beautiful and Scalable Gr

    Android SVG 使用是指在 Android 平台上使用 SVG(Scalable Vector Graphics)文件。SVG 是一种 XML 格式的图像文件,具有良好的可伸缩性,可以根据不同的分辨率自动适应。…

    2023-03-10 06:56:01
    0 75 20
  • androidx86下载 安装和运行Android操作系统在您的PC上

    Android-x86是一个开源项目,旨在将Google的Android操作系统移植到x86架构的笔记本电脑、台式机和其他设备上。它使用Linux内核,而不是Android原生的Linux内核,因此它可以在更多的硬件设备上运行。…

    2023-02-10 15:29:10
    0 44 87
  • android tcp连接:如何在Android上使用TCP连接

    Android TCP连接是一种使用TCP协议来在Android设备上实现网络通信的方式。它可以帮助Android应用程序与服务器之间进行双向通信,从而实现数据的传输和交换。…

    2023-03-08 14:34:39
    0 28 11
  • android ttf字体:Welcome

    Android ttf字体是指TrueType字体,是一种常见的字体格式,可以在Android系统中使用。它具有良好的可移植性和可扩展性,可以在不同的设备上显示出相同的文本效果。…

    2023-03-06 14:00:45
    0 65 34

发表评论

登录 后才能评论

评论列表(62条)