• 8

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I would like to call this method:

fun workingWithBtn(k: Int) {
        when (k) {
            1 -> {
                    this@LoginScr.runOnUiThread {
                }, 1000)
            2 -> {
            3 -> Handler().postDelayed({
                this@LoginScr.runOnUiThread {
            }, 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?

      • 2
    • it does not matter whether it is kotlin or java - you should never try to create an instance of Activity manually. Only system should create them. You have to find another way to call something in activity.
      • 2
    • @VladyslavMatviienko, I have made this via Java and right now I try to make it via kotlin, so I think that they have small differences
    • so, maybe you have some advice for me how to make what I want? because I would like to call activity method from singleton

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) {
registerReceiver(receiver, filter)

delete receiver when activity will be destroyed:

override fun onDestroy() {

maybe it will help someone else. Good Luck :)

  • 1
Reply Report