Toast does not appear in catch block

Hello! I am trying to show mesage when the network is off or the server is not responding. My messages are visible in the LOG, but do not appear on the screen (not fried). I have some sample code that works fine, but my code doesn't work.

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

How can I show my toast message on the screen? Thanks.

+4
source share
6 answers

You cannot do this. You can do something like this

 boolean flag=true;//take globally //working thread . . . catch (Exception e) { flag=false; Log.e(LOG_TAG, "CONNECTION ERROR FUNDAMO SERVER NOT RESPONDING", e); } 

Once your workflow has passed, check the flag value and show Toast.

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

Note. If you still want to show in NonUI Thread, you can use Handler or runOnUiThread()

+2
source

try it

 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
source

make sure you pass the correct context, for example:

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

check that this works for me

 @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
source

I am surprised that this has not yet been answered. It seems to me that all you need to do is run Toast in the UI thread. So in your catch block:

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

Declare globally write it in oncreate and show only in catch block.

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

All Articles