0

I am trying to create an instance of an activity in order to access methods in that class but whenever i call functions in that activity class I get this error: Attempt to invoke virtual method 'void. Here is my code

     public class SmsListActivity extends AppCompatActivity {
        
    
        private static SmsListActivity inst;
          public static SmsListActivity instance() {
    
            return inst;
        }
 @Override
    public void onStart() {
        super.onStart();
        inst = this;
    }
        }
 SmsListActivity inst =  SmsListActivity.instance();
user12504353
  • 179
  • 3
  • 13
  • 2
    `I am trying to create an instance of an activity` you should never ever be doing this, there's no scenario where you would ever need to create an instance of an activity like this – a_local_nobody Feb 11 '22 at 14:28
  • The activity contains an array adapter list which I need to update from an smsreceiver class. The funny thing is it used to until today. – user12504353 Feb 11 '22 at 14:31
  • Do you have an alternative on how I can update the list. – user12504353 Feb 11 '22 at 14:32
  • there are lots of proper ways of handling this. use one activity with fragments or move out the data so that the list itself is static, at least then you'll be able to update it, but you're looking for trouble by doing this – a_local_nobody Feb 11 '22 at 14:34
  • 1
    What monstruosity is this – cmak Feb 11 '22 at 14:35
  • I am learning. Check this https://stackoverflow.com/questions/48092371/android-how-to-create-the-instance-of-an-activity-class/48092519#:~:text=Activity%20instances%20are%20always%20created,describing%20the%20activity%20to%20start. – user12504353 Feb 11 '22 at 14:38
  • I see, if it's inside a Fragment, just call getActivity and check that is not null – cmak Feb 11 '22 at 15:37
  • You should add SMSReceiver class as well in the question. – Rohit Singh Feb 11 '22 at 16:17

0 Answers0