2017-09-30 4 views
-1

Also ich denke, ich habe ein paar Probleme mit der Problemdefinition. Ich versuche, die Funktion setZipEdit zu verwenden, um die R.id.zipHoldereditText in meiner Tätigkeit zu setzen. Ich habe nur Probleme beim Einrichten des Codes, um dies zu tun. Unten ist was ich habe was momentan nicht funktioniert. mZipHold kommt zurück als nicht initialized, die ich, obwohl es in der overrideProbleme beim Schreiben in EditText in Kotlin

AKTUALISIERT

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) 

} 
war

java.lang.NullPointerException: Der Versuch, virtuelle Methode ‚android.view.Window $ aufzurufen Callback android.view.Window.getCallback() 'auf einem Nullobjektreferenz um android.support.v7.app.AppCompatDelegateImplBase. (AppCompatDelegateImplBase.java:117) bei android.support.v7.app.AppCompatDelegateImplV9. (AppCompatDelegateImplV9.java:149) bei android.support.v7.app.AppCompatDelegateImplV11. (AppCompatDelegateImplV11.java:29) bei android.support.v7.app .AppCompatDelegateImplV14. (AppCompatDelegateImplV14.java:54) bei android.support.v7.app.AppCompatDelegateImplV23. (AppCompatDelegateImplV23.java:31) bei android.support.v7.app.AppCompatDelegateImplN. (AppCompatDelegateImplN.java:31) unter android.support.v7.app.AppCompatDelegate.create (AppCompatDelegate. Java: 198) bei android.support.v7.app.AppCompatDelegate.create (AppCompatDelegate.java:183) bei android.support.v7.app.AppCompatActivity.getDelegate (AppCompatActivity.java:519) bei android .support.v7.app.AppCompatActivity.findViewById (AppCompatActivity.java:190) bei

com.example.sdfsdf.listview.SecondActivity ._ $ _ findCachedViewById (SecondActivity.kt: 0) bei com.example .sdfsdf.listview.SecondActivity.setZipEdit (SecondActivity.kt: 44) bei com.example.sdfsdf.listview.SecondActivity $ MyCustomAdapter $ onCreateViewHolder $ 1.onClick (SecondActivity.kt: 124) bei android.view.View.performClick (View.java:6256) bei android.view.View $ PerformClick.run (View.java:24697) bei android.os.Handler.handleCallback (Handler.java:789) bei android.os.Handler.dispatchMessage (Handler.java:98) bei android.os.Looper .loop (Looper.java:164) bei android.app.ActivityThread.main (ActivityThread.java:6541) bei java.lang.reflect.Method.invoke (native Methode) bei com.android.internal.os.Zygote $ MethodeAndArgsCaller.run (Zygote.java:240) bei 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> 
+0

Warum verwenden Sie nicht Kotlinextension @BostonMacOSX – UltimateDevil

+0

Ich habe sie dort, aber Probleme mit der Abholung von Elementen, die ich in meine Layouts gesetzt habe. – BostonMacOSX

+0

können Sie Ihren Code mit Import oder XML-Datei hier aktualisieren – UltimateDevil

Antwort

1

Ok, aber kotlinextension verwendet, ist ein besserer Weg, dies zu tun, ich glaube, Sie 2 Dinge tun müssen, kann Ihr Problem

lösen erste

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

Ich denke, was Sie schon

zweite statt Initialisierung Ihre Editext durch findViewById direkt als

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

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

    } 

getan haben, können Sie Ihre Ansichten zugreifen, indem nur ihre ID verwenden. Lassen Sie es versuchen und lassen Sie mich wissen, wenn funktioniert

+0

Hallo ZipHolder.text erfordert Editierbar! aber zipEdit ist ein String..so setText (String) ist der Weg zu gehen. – BostonMacOSX

+0

Ja, ich habe meinen Code aktualisiert, wie Sie oben sehen, und @BostonMacOSX das funktioniert auf meiner Seite und hoffentlich wird das auch bei Ihnen funktionieren – UltimateDevil

+1

Sie brauchen nicht init Sicht innerhalb initialisieren Block. kotlin syntectic hat eine faule Initialisierung für Bind Views. –

Verwandte Themen