Emanuele
Emanuele

Reputation: 674

Toast message not showed and following code ignored

I have the following code:

protected MyCallback getMyCallbackHandler() {
    return new MyCallback() {

        @Override
        public void onReady() {
            // DO STUFF
            if (stuff!= null) {
                mThread = new MyThread(stuff,MyActivity.this);
                mThread.execute(mSensors.getLocation());
            }
            else {
                Toast.makeText(MyActivity.this, "No target detected! Try again later.", Toast.LENGTH_LONG).show();
                mSensors.registerCallback(null);
            }
            runOnUiThread(new Runnable(){

                @Override
                public void run() { 
                    comp.setVisibility(View.VISIBLE);
                }

            });
        }
    };
}

The problem is that if stuff==null when Toast.makeText(...).show()is called, first the toast message doesn't appear and second the next lines seems to be not executed. If I comment just the Toastline, everything run as expected.

I tried to put as context MyActivity.this, getParent().getApplicationContext()and also getApplicationContext()but it doesn't work.

Upvotes: 0

Views: 139

Answers (2)

Tarsem Singh
Tarsem Singh

Reputation: 14199

you can't use Toast here you should use Log.d() , if you really want to debug the applications start playing with Logcat !

if you want to use Toast inside this pass context to getMyCallbackHandler(Context c)

and use that in your Toast like

else {

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(c, "No target detected! Try again later.",
                            Toast.LENGTH_LONG).show();
                }

            });
            mSensors.registerCallback(null);
        }

Upvotes: 1

user2579943
user2579943

Reputation:

I think you have to make the call to Toast on the main UI thread.

Upvotes: 0

Related Questions