val receiptDetails = headerView.findViewById(R.id.tb_receipt_details) as TextView
val paymentDetails = headerView.findViewById(R.id.tb_payment_details) as TextView
val invoiceDetails = headerView.findViewById(R.id.tb_invoice_details) as TextView
val summary = headerView.findViewById(R.id.tb_summary) as TextView
tabLayout!!.getTabAt(0)!!.setCustomView(receiptDetails)
tabLayout!!.getTabAt(1)!!.setCustomView(paymentDetails)
tabLayout!!.getTabAt(2)!!.setCustomView(invoiceDetails)
tabLayout!!.getTabAt(3)!!.setCustomView(summary)
tabLayout!!.getChildAt(1).setEnabled(false)
在调用 tabLayout !!。getChildAt(1).setEnabled(false)时,出现了空指针异常。如何禁用Kotlin中的特定标签。预先感谢。
logcat
12-03 10:03:27.536 7899-7899/com.salesrep.auxxa.alliontechnologies.auxxasr E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.salesrep.auxxa.alliontechnologies.auxxasr, PID: 7899
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.salesrep.auxxa.alliontechnologies.auxxasr/com.salesrep.auxxa.alliontechnologies.auxxasr.NewPaymentActivity}: kotlin.KotlinNullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: kotlin.KotlinNullPointerException
at com.salesrep.auxxa.alliontechnologies.auxxasr.NewPaymentActivity.onCreate(NewPaymentActivity.kt:84)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
答案 0 :(得分:1)
由于您使用的是自定义视图,因此可以通过使用touch事件来禁用该标签:
val view = tabLayout.getTabAt(1)!!.customView.parent as View;
view.setOnTouchListener { v, event -> true }
答案 1 :(得分:0)
使用此:
(tabLayout!!.getChildAt(0) as ViewGroup).getChildAt(1).setEnabled(false)
tabLayout!!.getChildAt(0) as ViewGroup
是包含子视图的ViewGroup
。
答案 2 :(得分:0)
宣布活动
var touchableList: ArrayList<View>? = ArrayList()
禁用所有标签
touchableList = tabLayout?.touchables
touchableList?.forEach { it.isEnabled = false }
启用标签
touchableList?.get(0)?.isEnabled = true
答案 3 :(得分:-2)
更改
tabLayout !!。getChildAt(1).setEnabled(false)
收件人
tabLayout !!。getChildAt(1).isEnabled = false
活动未在清单中注册。注册。