I want to call the activity method under certain conditions from singleton. Here is the method itself:

fun workingWithBtn(k: Int) { when (k) { 1 -> { btn_submit_t.showError(); Handler().postDelayed({ this@LoginScr.runOnUiThread { btn_submit_t.hideLoading() btn_submit_t.isEnabled } }, 1000) } 2 -> { btn_submit_t.showSuccess() } 3 -> Handler().postDelayed({ clickCount-- this@LoginScr.runOnUiThread { btn_submit_t.hideLoading() btn_submit_t.isEnabled } }, 1000) } } 

In order to call it, I have registered activity in the singleton:

 public void setLoginScr(LoginScr loginScr) { this.loginScr = loginScr; } 

and in activity:

 ms.setLoginScr(LoginScr()) 

When calling, knocks out with an error:

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference 

The essence of the method is that it interacts with the elements of the screen. Judging by the error, I have not initialized the element I need. I tried after initializing by id to try to do something with this view, but I also failed. How can I fix this error?

  • one
    I personally did not understand anything, on top of the cotlin then java. The activation call is made with the startActivity (intent) command, I think you know about it. Personally, I do not understand anything. - Astend Sanferion
  • I want to call not activity from the intent, but the activity method - Andrew Goroshko
  • On java, everything turned out, but for some reason it doesn't work with Kotlin :( - Andrew Goroshko

1 answer 1

Solved my problem through BroadCastReceiver. To do this, in the singleton, where you need (I have when the condition is triggered) add:

 Intent intent = new Intent(); intent.setAction("btn_task"); // тут название вашего фильтра intent.putExtra("url", 1); context.sendBroadcast(intent); // здесь context не нужен вообще, но так как это вызов из синглтона то пришлось вставить 

Further in the activity we create a variable:

 lateinit var receiver: BroadcastReceiver 

assign it a value:

  val filter = IntentFilter("btn_task") 

and register the "recipient":

 registerReceiver(receiver, filter) 

catching data:

 receiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { workingWithBtn(intent.extras.getInt("url")) } } 

delete the "recipient" in the destruction of the activity:

 override fun onDestroy() { super.onDestroy() unregisterReceiver(receiver) } 

something like this :)