吐司没有在catch catch中显示

问题描述:

嗨!我试图在网络关闭或服务器没有响应时显示消息。我的消息在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

你不能那样做。你可以这样做

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://*.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

确保您传递正确的环境,例如:

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

试试这个

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

检查它对我来说工作正常

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

我很惊讶这还没有得到回答。在我看来,你所需要做的就是在UI线程上运行Toast。因此,在您的catch块中:

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

声明全局写入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(); 
      }