android-studio - Unresolved reference : kotlinx

我正在尝试在 Android Studio 中试用 Kotlin 和 Kotlin Android 扩展。我在 Ubuntu 14.04 上的 Android Studio v 1.5.1 和 OS X El Capitan 上的 Android Studio v 1.5.1 中都尝试过,结果相同。

这是我正在做的事情:

  1. 我安装了 Kotlin 插件 1.0.0-beta-35950-IJ141-11
  2. 创建一个新的空白 Android 项目
  3. 将 MainActivity 文件转换为 Kotlin(通过 help->findaction->convert file to kotlin)
  4. 为 Kotlin 配置项目

然后我进入生成的 content_main.xml 文件并为“Hello World!”添加一个 id (hello)。 TextView 。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.gmail.npnster.mykotlinfirstproject.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/hello"
        />
</RelativeLayout>  

然后在转换后的 MainActivity 中添加一行来设置 TextView。 (如下所示)。 Android Studio 然后提示我(通过 alt-enter)插入这一行(如下所示)

import kotlinx.android.synthetic.main.content_main.*

所以在这一点上,一切似乎都很好

但是当我尝试编译这个时,我得到了

Unresolved reference: kotlinx
Unresolved reference: kotlinx
Unresolved reference: hello

请注意,我没有安装了 Kotlin Android 扩展插件。几天前,它现在应该包含在主插件中,并被标记为过时。 (事实上​​如果你在有最新插件的情况下尝试安装它,并没有安装任何新插件)

有人看到我做错了吗?

主事件

import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.View
import android.view.Menu
import android.view.MenuItem
import kotlinx.android.synthetic.main.content_main.*


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val toolbar = findViewById(R.id.toolbar) as Toolbar
        setSupportActionBar(toolbar)
        print("setting text view value to hey")
        hello.text = "hey"

        val fab = findViewById(R.id.fab) as FloatingActionButton
        fab.setOnClickListener { view -> Snackbar.make(view, "Replace this with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show() }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        val id = item.itemId

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true
        }

        return super.onOptionsItemSelected(item)
    }
}

最佳答案

在我们的 buildscript 的依赖项中添加 kotlin-android-extensions:

1.在您的项目级 build.gradle

buildscript {
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
    }
}

并应用 kotlin-android-extensions 插件:

2。在你的模块级 build.gradle

apply plugin: 'kotlin-android-extensions'

关于android-studio - Unresolved reference : kotlinx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34169562/

相关文章:

android-studio - Android Studio 3.0 - 找不到方法 'com.a

kotlin - Kotlin 中等效的 Swift 'if let' 语句

kotlin - Kotlin 中的单个感叹号

syntax - Kotlin 二级构造函数

kotlin - Kotlin 中的多变量 let

kotlin - Kotlin中折叠和减少之间的区别,何时使用?

generics - Kotlin 中的 reified 关键字是如何工作的?

kotlin - bool 值的使用?在 if 表达式中

enums - Kotlin 中具有反向查找的有效枚举?

generics - 如何在 Kotlin 中将 TypeToken + 泛型与 Gson 一起使用