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
有帮助吗?

解决方案

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();
  }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top