0

i am really new to Android and i was trying to use the Thread class with a message handler, in there i need to use the ApplicationContext but when i try to run it it crashes, here is the code that makes the application crash

   if (!connected.isState()) {
                client = new MqttAndroidClient(myContext.context, SERVERURI, CLIENTID);
                try {
                    IMqttToken token = client.connect();
                    token.setActionCallback(new IMqttActionListener() {
                        @Override
                        public void onSuccess(IMqttToken asyncActionToken) {
                            //we are connected
                            connected.setState(true);
                        }

                        @Override
                        public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                            //we are not connected
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return;    
           }

here is the myContext class

class myContext extends Application {

    public static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
    }

} what can i do to fix the problem?

alemomo
  • 131
  • 1
  • 1
  • 6
  • "makes the application crash" is not an error description and this is bad practice anyway. – Martin Zeitler Jun 09 '22 at 22:11
  • yes, i know, but i cannot use the logcat in android studio because the serial port is used to comunicate with an arduino. – alemomo Jun 09 '22 at 22:15
  • @MartinZeitler OP is new here. Closing the question as duplicate is not helpful. Storing application context in a static variable is not generating any memory leak, as it is a singleton and never gets GC'd anyway. None of this is helpful to OP. – David Wasser Jun 09 '22 at 22:47
  • @MartinZeitler also the top answer with 1400 upvotes) to this question proposes exactly what OP has done: https://stackoverflow.com/questions/2002288/static-way-to-get-context-in-android/5114361#5114361 – David Wasser Jun 09 '22 at 22:52

2 Answers2

0

You probably haven't told Android to use your custom Application class, so myContext.onCreate() isn't being called. To do this you need to add this to your <application> declaration in your manifest:

android:name=".myContext"
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • hi, thanks for youranswer, i added it, now the application won't launch and i get the Illegal Access Exception, is it normal, what am i missing? – alemomo Jun 09 '22 at 22:12
  • I need to know more. I would need the exception message and stack trace – David Wasser Jun 09 '22 at 22:43
0

OP here. in the end i solved it by sending a message containing the applicationContext in message.obj, here is the code now

if (!connected.isState()) {
            client = new MqttAndroidClient((Context) msg.obj, SERVERURI, CLIENTID);
            try {
                IMqttToken token = client.connect();
                token.setActionCallback(new IMqttActionListener() {
                    @Override
                    public void onSuccess(IMqttToken asyncActionToken) {
                        //we are connected
                        connected.setState(true);
                    }

                    @Override
                    public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                        //we are not connected
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return;
    }

thanks to everybody for the suggestions and for keeping up with my inexperience

:-)

alemomo
  • 131
  • 1
  • 1
  • 6