2012-07-12 90 views
4

嗨!我試圖在網絡關閉或服務器沒有響應時顯示消息。我的消息在LOG中可見,但不顯示在屏幕上(未烤)。我有一個示例代碼工作正常,但我的代碼不是。吐司沒有在catch catch中顯示

import android.view.View.OnKeyListener; 

public class AgAppHelperMethods extends Activity { 

    private static final String LOG_TAG = null; 
    private static AgAppHelperMethods instance = null; 
    public static String varMobileNo; 
    public static String varPinNo; 

    String[][] xmlRespone = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.agapphelpermethods); 
    } 

    protected AgAppHelperMethods() {} 

    public static AgAppHelperMethods getInstance() 
    { 
     if(instance == null) 
     { 
      instance = new AgAppHelperMethods(); 
     } 
     return instance; 
    } 

    public static String getUrl() 
    { 
     String url = "https://demo.accessgroup.mobi/"; 
     return url; 
    } 

    public String[][] AgAppXMLParser(String parUrl) 
    {  
     String _node,_element; 
     String[][] xmlRespone = null; 
     try { 
      String url = AgAppHelperMethods.getUrl() + parUrl; 
      URL finalUrl = new URL(url); 

      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      Document doc = db.parse(new InputSource(finalUrl.openStream())); 
      doc.getDocumentElement().normalize(); 

      NodeList list=doc.getElementsByTagName("*"); 
      _node=new String(); 
      _element = new String(); 
      xmlRespone = new String[list.getLength()][2]; 

      for (int i=0;i<list.getLength();i++) 
      { 
       Node value=list.item(i).  getChildNodes().item(0); 
       _node=list.item(i).getNodeName(); 
       _element=value.getNodeValue(); 
       xmlRespone[i][0] = _node; 
       xmlRespone[i][1] = _element; 
      } 
     } 
     catch (Exception e) 
     { 
      Toast.makeText(getApplicationContext(), "error server not responding " + e.getMessage(), 
       Toast.LENGTH_SHORT).show(); 
      Log.e(LOG_TAG, "CONNECTION ERROR FUNDAMO SERVER NOT RESPONDING", e); 
     } 
    } 
} 

如何在屏幕上顯示我的敬酒信息?謝謝。

+0

嘗試使用'AgAppHelperMethods.this',而不是'getApplicationContext()'在'Toast' – Praveenkumar 2012-07-12 06:49:14

+1

請不要問人們對於他們的電子郵件地址。這裏沒有人是你的私人幫手。 – 2012-07-12 07:20:19

回答

2

你不能那樣做。你可以這樣做

boolean flag=true;//take globally 

//working thread 
. 
. 
. 

    catch (Exception e) 
    { 
    flag=false; 
    Log.e(LOG_TAG, "CONNECTION ERROR FUNDAMO SERVER NOT RESPONDING", e); 
    } 

一旦你的工作線程結束檢查標誌值並顯示Toast。

//Main Thread 
if(!flag) 
Toast.makeText(getApplicationContext(), "error server not responding " + e.getMessage(), 
       Toast.LENGTH_SHORT).show(); 

注:如果您仍然希望在NonUI主題展示,那麼你可以使用HandlerrunOnUiThread()

+0

爲什麼*不能在'catch'中完成?我看到這裏沒有邏輯差異或額外的邏輯的理由.. – 2012-07-12 06:53:26

+0

@pst http://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare他正在從工作線程更新UI。他需要從主線程中調用它。 – 2012-07-12 06:56:24

+0

..不相關。討論*不同線程*上的問題,其中上面的代碼*不會改變*。 – 2012-07-12 06:57:16

0

確保您傳遞正確的環境,例如:

Toast.makeText(MyActivity.this , "error server not responding " + e.getMessage(), 
       Toast.LENGTH_SHORT).show(); 
1

試試這個

Toast.makeText(AgAppHelperMethods.this, "error server not responding " + e.getMessage(), 
       Toast.LENGTH_SHORT).show(); 
    Log.e(LOG_TAG, "CONNECTION ERROR FUNDAMO SERVER NOT RESPONDING", e); 
+1

是否仍然有問題?你可以在這裏發佈你的代碼,我們都想幫你。 – 2012-07-12 07:36:50

0

檢查它對我來說工作正常

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_location_finder); 

     show();    
    } 

    public void show() 
    { 
     try 
     { 
      throw new ArrayIndexOutOfBoundsException() ; 
     } 

     catch(Exception e) 
     { 
      Toast.makeText(getApplicationContext(), "HI", Toast.LENGTH_LONG).show(); 
     } 
    } 
} 
0

我很驚訝這還沒有得到回答。在我看來,你所需要做的就是在UI線程上運行Toast。因此,在您的catch塊中:

runOnUiThread(new Runnable(){ 
    Toast.makeText(...); 
}); 
0

聲明全局寫入oncreate並只顯示在catch塊中。

Toast toast; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity); 

     toast = Toast.makeText(ActivityDeliverables.this, "Server is not working, please contact with admin.", Toast.LENGTH_LONG); 
    } 


      try{ 
      } catch (Exception e) { 

       toast.show(); 
      }