简体   繁体   中英

TextView Null after inflating parent layout

TextView Null after inflating parent layout. I'm running a loop which inflates a number of these layouts:

for(i in ticketAr?.indices!!){

    var inf1:LayoutInflater = LayoutInflater.from(context)
    var infv:View = inf1.inflate(R.layout.ticket_individual,null,false)
    infv.setId(parseInt(ticketAr[i].string("ntb_id")))
    var thetitle = infv.tck_event_title
    thetitle.setText(ticketAr[i].string("age_group_desc"))
}

STACKTRACE

10-25 09:30:35.839 11549-11549/com.example.xx.listview
E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.xx.listview, PID: 11549
  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

  at
com.example.xx.listview.Ordering_Setup$getData$1.invoke(Ordering_Setup.kt:145)
  at
com.example.xx.listview.Ordering_Setup$getData$1.invoke(Ordering_Setup.kt:29)
  at
com.github.kittinunf.fuel.core.DeserializableKt$response$1.invoke(Deserializable.kt:37)
  at
com.github.kittinunf.fuel.core.DeserializableKt$response$1.invoke(Deserializable.kt)
  at
com.github.kittinunf.fuel.core.DeserializableKt$response$5$1.invoke(Deserializable.kt:62)
  at
com.github.kittinunf.fuel.core.DeserializableKt$response$5$1.invoke(Deserializable.kt)
  at
com.github.kittinunf.fuel.core.Request$callback$1.run(Request.kt:225)
   at android.os.Handler.handleCallback(Handler.java:751)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6077)
   at java.lang.reflect.Method.invoke(Native Method)
   at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

You're not using findViewById() anywhere in your code. Instead of doing:

 var thetitle = infv.tck_event_title

Try this:

 var thetitle = infv.findViewById(R.id.tck_event_title) as TextView

Or the equivalent ID of that TextView you're trying to use.

Also, you're creating a new LayoutInflater on each loop. This line of code:

var inf1:LayoutInflater = LayoutInflater.from(context)

Should really be outside the loop (And can even be a val instead of a var ).

Edit: If you were trying to use the Kotlin Android Extensions, I suggest that you check this answer . You might've forgotten to import something.

Not really and answer other then the original code does work.

What had happened is that Android Studio reverted my custom layout names to the generic Textview7/8 etc etc. The GUI was showing my custom names but the XML behind the scenes was showing the generic names. Reported it as a bug to the developer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM