java - Cannot make a static reference / Cannot use "this" in static context Dilemma -


i'm attempting create android application accomplishes following: sends initial survey question via text message group of recipients (numbers stored in array). each response gets, sends participant next question in series. first android development i've ever done, apologies if obvious!

i have sendquestion function defined such within mainactivity:

public void sendquestion(string number, int question) {     //necessary setup sending text message     smsmanager sms = smsmanager.getdefault();     pendingintent sentpi;     string sent = "sms_sent";     sentpi = pendingintent.getbroadcast(this, 0, new intent(sent), 0);      //actually sends text message     sms.sendtextmessage(number, null, questions[question], sentpi, null); } 

then i'm attempting call sendquestion function within smsreceiver class:

public class smsreceiver extends broadcastreceiver {      @override     public void onreceive(context context, intent intent)      {         (int i=0;i<mainactivity.numbers.length;i++)         {             mainactivity.sendquestion(mainactivity.numbers[i], mainactivity.survey_location[i]);         }     } } 

the first error can't make static reference non-static method sendquestion type mainactivity. fix, thought i'd try changing sendquestion static method. error on sendpi within sendquestion cannot use this in static context.

is there way can mainactivity's context or keep sendquestion non-static without conflicting whichever 1 don't change? , what's best way avoid type of problem in general in future?

recommended way make utility methods sendquestion static in different class , send context parameter method. in way can access method , need pass context additional parameter method.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -