4

你好 !当网络关闭或服务器没有响应时,我试图显示一条消息。我的消息在 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);
        }
    }
}

如何在屏幕上显示我的 toast 消息?谢谢。

4

7 回答 7

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

注意:如果您仍想在非 UI 线程中显示,则可以使用HandlerrunOnUiThread()

于 2012-07-12T06:51:16.823 回答
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);
于 2012-07-12T07:00:59.017 回答
0

I'm surprised this hasn't been answered yet. It appears to me all you need to do is run the Toast on the UI thread. Thus, in your catch block:

runOnUiThread(new Runnable(){
    Toast.makeText(...);
});
于 2012-09-24T14:31:01.330 回答
0

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

Toast.makeText(MyActivity.this , "error  server not responding " + e.getMessage(),
                Toast.LENGTH_SHORT).show();
于 2012-07-12T06:56:57.743 回答
0

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

 getActivity().runOnUiThread(Runnable { Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show() })
于 2018-06-28T11:37:42.753 回答
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();
            }
于 2017-07-21T13:18:09.937 回答
-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();
         }
    }
}
于 2012-07-12T07:04:43.970 回答