2014-01-15 110 views
0

有沒有人有一個想法,爲什麼以下吐司不出現? 日誌被印刷在同一功能的另一土司也正在吐司消息不服務意圖

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)); 
      }   
    } 

任何幫助將不勝感激

我附加日誌,我不得不刪除一些行,因爲它太長了.. 。 這裏是日誌:

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 
+1

試着給自己的活動範圍內,而不是getApplicationContext。 –

+1

你在哪裏試圖展示它?我的意思是,它是否在一個活動? – nKn

+0

不可能,如果你的日誌**返回狀態真/假 - > **打印出來,然後用你爲Toast顯示的代碼必須執行,會出錯。 – user370305

回答

0

我認爲它在Android框架的錯誤。每當你嘗試製作或顯示一個吐司應用程序主線程。 IntentService大部分可能。始終確保您的吐司進入應用程序主線程。

看看下面的解決方法。這一切代碼都在您的服務中。

處理程序聲明。

Handler handler = null; 

,並在您的服務的構造

handler = new Handler(); 

方法來調用顯示吐司。

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(); 
    } 
+0

看看我更新的答案。當我發現問題的原因時,您可以從上述問題中刪除您的評論。 – user370305

+0

其工作!!!!!!很多! – Lora

+0

只是爲了好奇....任何想法爲什麼其他吐司在同一服務工作正常? – Lora