我想嘗試在Android Studio中使用Kotlin和Kotlin Android擴展。我已經在Ubuntu 14.04上的Android Studio v 1.5.1以及OS X El Capitan上的Android Studio v 1.5.1上嘗試了這一點,獲得了相同的結果。未解決的參考:kotlinx
下面是我在做什麼:
- 我安裝插件科特林1.0.0-β-35950-IJ141-11
- 創建一個新的空白Android項目
- 在MainActivity文件轉換科特林(通過幫助 - > findaction->轉換文件科特林)
- 配置項目科特林
然後我進入產生CON tent_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
注意,我並沒有安裝的科特林的Android擴展插件。就在幾天前,現在這應該被包含在主插件中,並被標記爲過時。 (事實上,如果您在安裝最新插件時嘗試安裝它,則不會安裝任何新軟件)
任何人都看到我在做什麼錯了?
MainActivity
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)
}
}
這就是我所做的,但我仍然得到1.0.0-beta-3595的問題:( –
不適用於我(1.0.0-beta-4589) - 任何解決方案嗎?只需添加評論在bug bug跟蹤器中出現錯誤 –
我在1.0.0-beta-4589上也有這個問題,你還必須在你的build.gradle文件中添加'apply plugin:'kotlin-android-extensions'' –