在Kotlin中写入EditText时遇到问题

时间:2017-09-30 18:27:27

标签: android android-edittext kotlin

所以我认为我有一些范围问题。 我尝试使用 setZipEdit 功能在我的活动中设置R.id.zipHolder editText。我在设置代码方面遇到了问题,无法做到这一点。以下是我目前没有的工作。 mZipHold回归initialized而不是我override

已更新

class SecondActivity : AppCompatActivity() {
lateinit var townInfo:JsonArray<JsonObject>


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity2)


    SET_TOWN.setOnClickListener {v -> setTowns()}
    GET_TOWNS.setOnClickListener {v -> retrieveTowns()}


}
 fun setZipEdit(zipEdit:String){
    Log.d("SZE",zipEdit)
     zipHolder.setText(zipEdit)



     //mZipHold.setText(zipEdit,TextView.BufferType.EDITABLE)

}
  

java.lang.NullPointerException:尝试调用虚方法   &#39; android.view.Window $ Callback android.view.Window.getCallback()&#39;在...上   null对象引用                                                                                      在   android.support.v7.app.AppCompatDelegateImplBase。(AppCompatDelegateImplBase.java:117)                                                                                      在   android.support.v7.app.AppCompatDelegateImplV9。(AppCompatDelegateImplV9.java:149)                                                                                      在   android.support.v7.app.AppCompatDelegateImplV11。(AppCompatDelegateImplV11.java:29)                                                                                      在   android.support.v7.app.AppCompatDelegateImplV14。(AppCompatDelegateImplV14.java:54)                                                                                      在   android.support.v7.app.AppCompatDelegateImplV23。(AppCompatDelegateImplV23.java:31)                                                                                      在   android.support.v7.app.AppCompatDelegateImplN。(AppCompatDelegateImplN.java:31)                                                                                      在   android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:198)                                                                                      在   android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)                                                                                      在   android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)                                                                                      在   android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)                                                                                      在

     

com.example.sdfsdf.listview.SecondActivity ._ $ _ findCachedViewById(SecondActivity.kt:0)                                                                                      在   com.example.sdfsdf.listview.SecondActivity.setZipEdit(SecondActivity.kt:44)                                                                                      在   com.example.sdfsdf.listview.SecondActivity $ MyCustomAdapter $ onCreateViewHolder $ 1.onClick(SecondActivity.kt:124)                                                                                      在android.view.View.performClick(View.java:6256)                                                                                      在android.view.View $ PerformClick.run(View.java:24697)                                                                                      在android.os.Handler.handleCallback(Handler.java:789)                                                                                      在android.os.Handler.dispatchMessage(Handler.java:98)                                                                                      在android.os.Looper.loop(Looper.java:164)                                                                                      在android.app.ActivityThread.main(ActivityThread.java:6541)                                                                                      at java.lang.reflect.Method.invoke(Native Method)                                                                                      在   com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

XML

<?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"
    android:background="#FFFFFF"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="25dp">

    <ImageView
        android:id="@+id/returnimage"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="20dp"
        android:contentDescription="logoforZip"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_new_icon_plain" />

    <EditText
        android:id="@+id/zipHolder"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:inputType="number"
        android:text="ZIP HERE"
        android:textAlignment="center"
        android:textColor="#000000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/returnimage"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/SET_TOWN"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginTop="8dp"
        android:text="SET TOWN"
        app:layout_constraintEnd_toEndOf="@+id/zipHolder"
        app:layout_constraintStart_toStartOf="@+id/zipHolder"
        app:layout_constraintTop_toBottomOf="@+id/zipHolder" />

    <Button
        android:id="@+id/GET_TOWNS"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginTop="8dp"
        android:text="LIST NEARBY TOWNS"
        app:layout_constraintEnd_toEndOf="@+id/SET_TOWN"
        app:layout_constraintStart_toStartOf="@+id/SET_TOWN"
        app:layout_constraintTop_toBottomOf="@+id/SET_TOWN" />

    <View
        android:id="@+id/splitbar"
        android:layout_width="fill_parent"
        android:layout_height="6dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:background="#c0c0c0"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/GET_TOWNS" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/zipList"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="#222222"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/splitbar" />

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:1)

好的,但使用kotlinextension是一种更好的方法,我认为你需要做两件事可能会解决你的问题

首先

import kotlinx.android.synthetic.main.activity_main2.*

我认为你已经完成了

,而不是EditextfindViewById直接使用

初始化
fun setZipEdit(zipEdit:String){
        Log.d("SZE",zipEdit)
        Log.d("mzh",mZipHold.toString())
         zipHolder.setText(zipEdit)

         //mZipHold.setText(zipEdit,TextView.BufferType.EDITABLE)

    }

您只需使用其ID即可访问您的观看次数。试试这个,让我知道是否有效