我在Android Studio中收到此错误。我只想在按下按钮时打印文本。我得到以下错误,每次按下按钮时都会出现错误。如果我取消注释意图它也可以完美地运行,但是我拥有的代码越多,错误就越多。我不确定这是真正的错误。我说这是因为我有一个完整的应用程序(不是这个)使用retrofit和github api来搜索repos。此按钮是搜索的关键。如果我硬编码一个单词,该应用程序完美。我终于得到了这个简化的应用程序,试图找到根本原因。请帮忙。
1。
10-17 15:33:56.569 1404-1431/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 2045606 , only wrote 1793520
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val editText = findViewById<EditText>(R.id.searchEditText)
val searchEditText = editText.text.toString()
val button = findViewById<Button>(R.id.searchButton)
button.setOnClickListener {
println(searchEditText)
//val intent = Intent(this,SearchResultActivity::class.java)
//intent.putExtra("searchTerm",searchEditText)
//startActivity(intent)
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.blah.test236">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SearchResultActivity" />
</application>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context="com.example.dnarialpressley.test236.MainActivity"
tools:ignore="DuplicateIds">
<EditText
android:id="@+id/searchEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchEditText" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:2)
XML很好,但主要活动类没有以正确的方式使用kotlin,为了从您的布局绑定您的项目,您可以使用合成本机库这样
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.* // this imports all your items from your layout
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val searchEditText = searchEditText.text.toString()
// you can now use any items in the layout just by calling them
searchButton.setOnClickListener{
println(searchEditText)
}
}
}
希望有所帮助
答案 1 :(得分:0)
将您的班级代码更改为此
val editText = findViewById<EditText>(R.id.searchEditText)
val searchEditText = editText.text.toString()
val button = findViewById<Button>(R.id.searchButton)
button.setOnClickListener {
println(searchEditText)
//val intent = Intent(this,SearchResultActivity::class.java)
//intent.putExtra("searchTerm",searchEditText)
//startActivity(intent)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
在这里,我们刚刚将“ override”语句移到了最后。我遇到了类似的问题,但是在Java中,当我将重写功能移到底部时,我再也没有看到该问题。我希望它也适用于Kotlin。