0

does anyone has an idea why the following toast is not appearing ? the log is printed and another toast in the same function is also working

    if (searchType.equals("ByName")){
        // get the extra from the intent:
        String query = intent.getStringExtra("query");      
            if (!searchByName(query)){
                Toast.makeText(getApplicationContext(), "No results for this search",
                        Toast.LENGTH_LONG).show();  
                Log.d(TAG, "return status true/false -->" + searchByName(query));
            }           
    }

any help will be appreciated

i'm attaching the Log, i had to remove some lines since it's too long... here is the Log:

01-15 10:22:39.267: D/FragmentList(2035): doSearchByName
01-15 10:22:39.267: D/FragmentList(2035): query -> asdgasdgsdagas
01-15 10:22:39.271: D/FragmentList(2035): ByName
01-15 10:22:39.283: D/FragmentList(2035): currentLocation
01-15 10:22:39.303: D/SearchPlacesSevice(2035): onHandleIntentstart
01-15 10:22:39.303: D/SearchPlacesSevice(2035): ByName
01-15 10:22:39.303: D/SearchPlacesSevice(2035): searchByName
01-15 10:22:39.303: D/GooglePlaces(2035): query -> asdgasdgsdagas
01-15 10:22:39.307: D/GooglePlaces(2035): https://maps.googleapis.com/maps/api/place/textsearch/json?query=asdgasdgsdagas&sensor=false&key=AIzaSyBDi3A3ZLFQPm1I9sUsYasxtwndQjn7mfQ
01-15 10:22:39.879: D/GooglePlaces(2035): {
01-15 10:22:39.879: D/GooglePlaces(2035):    "debug_info" : [],
01-15 10:22:39.879: D/GooglePlaces(2035):    "html_attributions" : [],
01-15 10:22:39.879: D/GooglePlaces(2035):    "results" : [],
01-15 10:22:39.879: D/GooglePlaces(2035):    "status" : "ZERO_RESULTS"
01-15 10:22:39.879: D/GooglePlaces(2035): }
01-15 10:22:39.879: D/SearchPlacesSevice(2035): {
01-15 10:22:39.879: D/SearchPlacesSevice(2035):    "debug_info" : [],
01-15 10:22:39.879: D/SearchPlacesSevice(2035):    "html_attributions" : [],
01-15 10:22:39.879: D/SearchPlacesSevice(2035):    "results" : [],
01-15 10:22:39.879: D/SearchPlacesSevice(2035):    "status" : "ZERO_RESULTS"
01-15 10:22:39.879: D/SearchPlacesSevice(2035): }
01-15 10:22:39.879: D/SearchPlacesSevice(2035): ZERO_RESULTS
01-15 10:22:39.883: D/SearchPlacesSevice(2035): Status -->ZERO_RESULTS
01-15 10:22:39.887: D/SearchPlacesSevice(2035): searchByName
01-15 10:22:39.887: D/GooglePlaces(2035): query -> asdgasdgsdagas
01-15 10:22:39.887: D/GooglePlaces(2035): https://maps.googleapis.com/maps/api/place/textsearch/json?query=asdgasdgsdagas&sensor=false&key=AIzaSyBDi3A3ZLFQPm1I9sUsYasxtwndQjn7mfQ
01-15 10:22:40.083: D/SearchPlacesSevice(2035): {
01-15 10:22:40.083: D/SearchPlacesSevice(2035):    "debug_info" : [],
01-15 10:22:40.083: D/SearchPlacesSevice(2035):    "html_attributions" : [],
01-15 10:22:40.083: D/SearchPlacesSevice(2035):    "results" : [],
01-15 10:22:40.083: D/SearchPlacesSevice(2035):    "status" : "ZERO_RESULTS"
01-15 10:22:40.083: D/SearchPlacesSevice(2035): }
01-15 10:22:40.087: D/SearchPlacesSevice(2035): ZERO_RESULTS
01-15 10:22:40.087: D/SearchPlacesSevice(2035): Status -->ZERO_RESULTS
01-15 10:22:40.091: D/SearchPlacesSevice(2035): return status true/false -->false
01-15 10:22:43.387: W/MessageQueue(2035): Handler (android.os.Handler) {535afd88} sending message to a Handler on a dead thread
01-15 10:22:43.387: W/MessageQueue(2035): java.lang.RuntimeException: Handler (android.os.Handler) {535afd88} sending message to a Handler on a dead thread
01-15 10:22:43.387: W/MessageQueue(2035):   at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
01-15 10:22:43.387: W/MessageQueue(2035):   at android.os.Handler.sendMessageAtTime(Handler.java:473)
01-15 10:22:43.387: W/MessageQueue(2035):   at android.os.Handler.sendMessageDelayed(Handler.java:446)
01-15 10:22:43.387: W/MessageQueue(2035):   at android.os.Handler.post(Handler.java:263)
01-15 10:22:43.387: W/MessageQueue(2035):   at android.widget.Toast$TN.hide(Toast.java:363)
01-15 10:22:43.387: W/MessageQueue(2035):   at android.app.ITransientNotification$Stub.onTransact(ITransientNotification.java:55)
01-15 10:22:43.387: W/MessageQueue(2035):   at android.os.Binder.execTransact(Binder.java:367)
01-15 10:22:43.387: W/MessageQueue(2035):   at dalvik.system.NativeStart.run(Native Method)
01-15 10:23:42.003: D/dalvikvm(2035): GC_CONCURRENT freed 434K, 6% free 8502K/9031K, paused 1ms+1ms, total 8ms
Lora
  • 25
  • 1
  • 8
  • 1
    try giving your Activity context instead of getApplicationContext. – Padma Kumar Jan 15 '14 at 09:59
  • 1
    Where are you trying to show it? I mean, is it within an Activity? – nKn Jan 15 '14 at 09:59
  • Not Possible, Something going wrong if your log **return status true/false -->** printed then by code you shown for Toast must be executed. – user370305 Jan 15 '14 at 10:01
  • If you're running from within an Activity just use `this` instead of `getApplicationContext()` – Dreagen Jan 15 '14 at 10:01
  • @Tobor she said the Log line was showing – nKn Jan 15 '14 at 10:01
  • 2
    @All - Who are just suggest `this` instead of `getApplicationContext()` be cleared that, Toast can be work any where in Service also. Without Activity Context it need only Context. – user370305 Jan 15 '14 at 10:03
  • As far as I'm aware services don't run on the UI thread and a `Toast` would need to be. I will look and see if I can find something more about it – Dreagen Jan 15 '14 at 10:06
  • @Dreagen - Toast doesn't need UI Thread. – user370305 Jan 15 '14 at 10:08
  • @Lora are you executing that snippet within a Thread? – nKn Jan 15 '14 at 10:09
  • @user370305 Are you sure? Just found this: http://stackoverflow.com/questions/12730675/show-toast-at-current-activity-from-service – Dreagen Jan 15 '14 at 10:09
  • @user370305 toast doesnot work in background thread – Abhishek Agarwal Jan 15 '14 at 10:09
  • If she says one of the Toasts is displaying and the other doesn't, running it within a Service is clearly not the issue; there must be something different from one case to other – nKn Jan 15 '14 at 10:11
  • @user370305 - I always thought it needed to ran on the UI thread. I guess I must have got my wires crossed somewhere! – Dreagen Jan 15 '14 at 10:14
  • What Android version has the device you're trying to run it on? Or is it on a virtual device? – nKn Jan 15 '14 at 10:18
  • Have a look at this because it might be related: http://stackoverflow.com/questions/12159546/why-toast-message-are-not-show-in-android-4-1-operating-system-containing-mobile – nKn Jan 15 '14 at 10:24
  • **Toast should be run on Application's Main Thread**. – user370305 Jan 15 '14 at 11:40

1 Answers1

0

I think its a bug in Android Framework. Whenever you are try to make or display a Toast out of Application Main Thread. Mostly possible with IntentService. Always be sure your Toast goes in Application Main Thread.

Look at Below Workaround. This all code goes in your Service.

Handler Declaration.

Handler handler = null;

and in Constructor of your Service

handler  = new Handler();

Method to call for display Toast.

public void showToast(String message, Context context){
        handler.post(new CustomToast(message, context));
    }

CostomToast

private class CustomToast implements Runnable{
          String mText;
          Context mContext;

  public CustomToast(String text, Context context){
            mText = text;
            mContext = context;
  }

   public void run(){
            Toast.makeText(mContext, mText, Toast.LENGTH_LONG).show();
  }
user370305
  • 108,599
  • 23
  • 164
  • 151
  • Look at my updated answer. You can delete your comments from above question as I found the cause of problem. – user370305 Jan 15 '14 at 11:24
  • just for curiosity.... any idea why other toast in the same service is working properly ? – Lora Jan 15 '14 at 11:28
  • No its not Working Properly, Actually when First Toast called with hide() method it will hanged on Thread. So its look like first Toast running properly and other is not. – user370305 Jan 15 '14 at 11:30
  • i did not called a toast with hide ()... anyway, i will use your brilliant method for toasts in service from now (: – Lora Jan 15 '14 at 11:46
  • Actually, Application Manager called when Toast display time expired. My Answer only needed when you are using IntentService or Service which will run in independent thread not in Application's Main Thread. – user370305 Jan 15 '14 at 11:49