Search code examples
javaandroidkotlintimertask

Android kotlin - BottomNavigationView use BadgeView inside TimerTask


This is the code which works:

class Stackoverflow : AppCompatActivity() {

    private var menuView: BottomNavigationMenuView? = null
    private var mBottomNavigationView: BottomNavigationView? = null
    private var newmsgTimer: Timer? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)


        mBottomNavigationView = findViewById<View>(R.id.bottom_navigation) as BottomNavigationView
        menuView = mBottomNavigationView!!.getChildAt(0) as BottomNavigationMenuView


        mBottomNavigationView!!.setOnNavigationItemSelectedListener { item ->
            when (item.itemId) {
                R.id.nav_voting -> {

                }
                R.id.nav_search -> {

                }
            }
            return@setOnNavigationItemSelectedListener true
        }

        newmsg()
    }



    private fun newmsg(){
        QBadgeView(this@Stackoverflow).bindTarget(menuView!!.getChildAt(3)).badgeNumber = 7 // IT WORKS HERE

        newmsgTimer = Timer()

        val timerTask = object:TimerTask() {
            override fun run() {
                Log.d("letsSee", "PIKABOO") // it's getting printed

            }
        }

        newmsgTimer!!.schedule(timerTask, 0, 60000)
    }
}

But when I try to use QBadgeView(this@Home).bindTarget(menuView!!.getChildAt(3)).badgeNumber = 7 inside TimerTask:

private fun newmsg(){
    newmsgTimer = Timer()

    val timerTask = object:TimerTask() {
        override fun run() {
            Log.d("letsSee", "PIKABOO") // it's getting printed

            QBadgeView(this@Stackoverflow).bindTarget(menuView!!.getChildAt(3)).badgeNumber = 7 // DOESN'T WORKS HERE
        }
    }

    newmsgTimer!!.schedule(timerTask, 0, 60000)
}

then it doesn't work.

And I don't know what this error is about:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getVisibility()' on a null object reference
        at android.support.design.internal.BottomNavigationMenuView.onMeasure(BottomNavigationMenuView.java:145)

Any ideas how to make it work inside TimeTask? Thanks in advance

EDIT:

After another run I also got this error:

FATAL EXCEPTION: Timer-1
    Process: com.exmpl.exmpl, PID: 9978
    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

Ignore this: need to add more words blabla


Solution

  • You can not change view properties on another thread directly, you can only change on main thread.

    val timerTask = object : TimerTask() {
        override fun run() {
            Log.d("letsSee", "PIKABOO") // it's getting printed
    
            menuView.post{
    
          QBadgeView(this@Stackoverflow).bindTarget(menuView!!.getChildAt(3)).badgeNumber = 7
    
            }
        }