Toast没有显示在catch块中

时间:2012-07-12 06:48:27

标签: android toast

你好!我正在尝试在网络关闭或服务器没有响应时显示消息。我的消息在LOG中可见,但没有显示在屏幕上(没有烘烤)。我有一个示例代码可以正常工作,但我的代码不是。

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

如何在屏幕上显示我的祝酒词?感谢。

7 个答案:

答案 0 :(得分: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()

答案 1 :(得分: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);

答案 2 :(得分:0)

确保传递正确的上下文,例如:

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

答案 3 :(得分:0)

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

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

答案 4 :(得分: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();
            }

答案 5 :(得分:0)

如果有人仍然需要帮助,此方法对我有用:

 getActivity().runOnUiThread(Runnable { Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show() })

答案 6 :(得分:-1)

检查这对我的工作正常

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