Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

android - Having issues writing to EditText in Kotlin

So I think I'm having some scoping issues. I'm trying to use the function setZipEdit to set the R.id.zipHolder editText in my activity. I'm just having issues in setting up the code to be able to do this. Below is what I have which isn't currently working. mZipHold is coming back as not initialized which I though it was in the override

UPDATED

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: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:117) at android.support.v7.app.AppCompatDelegateImplV9.(AppCompatDelegateImplV9.java:149) at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:29) at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:54) at android.support.v7.app.AppCompatDelegateImplV23.(AppCompatDelegateImplV23.java:31) at android.support.v7.app.AppCompatDelegateImplN.(AppCompatDelegateImplN.java:31) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:198) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519) at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190) at

com.example.sdfsdf.listview.SecondActivity._$_findCachedViewById(SecondActivity.kt:0) at com.example.sdfsdf.listview.SecondActivity.setZipEdit(SecondActivity.kt:44) at com.example.sdfsdf.listview.SecondActivity$MyCustomAdapter$onCreateViewHolder$1.onClick(SecondActivity.kt:124) at android.view.View.performClick(View.java:6256) at android.view.View$PerformClick.run(View.java:24697) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, But using kotlinextension is a better way to do this, I think you need to do 2 things may solve your problem

first

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

I think which you have already done

second instead of initializing your Editext by findViewById direct use as

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

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

    }

you can access your views by only use their id. Let try this and let me know if works


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...