Reputation: 2157
I would like to call this method:
fun workingWithBtn(k: Int) {
when (k) {
1 -> {
btn_submit_t.showError();
Handler().postDelayed({
[email protected] {
btn_submit_t.hideLoading()
btn_submit_t.isEnabled
}
}, 1000)
}
2 -> {
btn_submit_t.showSuccess()
}
3 -> Handler().postDelayed({
clickCount--
[email protected] {
btn_submit_t.hideLoading()
btn_submit_t.isEnabled
}
}, 1000)
}
}
this method is placed at the kotlin-based activity and I would like to call it from java singleton. I call this method from singleton like this:
new LoginScr().workingWithBtn(3);
but I receive the error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
as I understand my class can't find my button. I tried to use findViewById and then work with btn but it didn't help me. How I can solve this problem?
Upvotes: 2
Views: 144
Reputation: 2157
I have managed to solve my problem via BroadcastReceiver. For this solution we have to add to our singleton to the place where we will need a function call these lines:
Intent intent = new Intent();
intent.setAction("btn_task"); // name of your filter
intent.putExtra("url", 1);
context.sendBroadcast(intent); // here you won't need context but I have to use it from singleton
then we create a variable at the activity:
lateinit var receiver: BroadcastReceiver
then we will assign the value:
val filter = IntentFilter("btn_task") // we will filter all intents with our filter
and then we have to create and register our receiver:
receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
workingWithBtn(intent.extras.getInt("url"))
}
}
registerReceiver(receiver, filter)
delete receiver when activity will be destroyed:
override fun onDestroy() {
super.onDestroy()
unregisterReceiver(receiver)
}
maybe it will help someone else. Good Luck :)
Upvotes: 1