0
        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)

Here I got a null pointer exception when I called tabLayout!!.getChildAt(1).setEnabled(false). How can I disable specific tab in Kotlin. Thanks in advance.

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)
Rajitha Perera
  • 1,581
  • 5
  • 26
  • 42

4 Answers4

1

Since you're using custom view, you could disable the tab by consuming the touch event:

val view = tabLayout.getTabAt(1)!!.customView.parent as View;
view.setOnTouchListener { v, event -> true }
Aaron
  • 3,764
  • 2
  • 8
  • 25
0

Use this:

(tabLayout!!.getChildAt(0) as ViewGroup).getChildAt(1).setEnabled(false)

tabLayout!!.getChildAt(0) as ViewGroup is the ViewGroup containing the children views.

forpas
  • 160,666
  • 10
  • 38
  • 76
0

declare the activity

var touchableList: ArrayList<View>? = ArrayList()

disable all tab

touchableList = tabLayout?.touchables
touchableList?.forEach { it.isEnabled = false }

Enable the tab

touchableList?.get(0)?.isEnabled = true
Arul Pandian
  • 1,685
  • 15
  • 20
-2

change From

tabLayout!!.getChildAt(1).setEnabled(false)

To

tabLayout!!.getChildAt(1).isEnabled = false

And Activity not register in manifest. register it.

Parth Suthar
  • 123
  • 4