4

Hi ! I'm trying to display a mesage when the network is off or the server is not responding. My messsage is visible in LOG but does not show on screen (is not toasted). I have a sample code which works fine but my code is not.

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.

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
user1513889
  • 211
  • 1
  • 3
  • 5
  • Try to use `AgAppHelperMethods.this` instead of `getApplicationContext()` in your `Toast` – Praveenkumar Jul 12 '12 at 06:49
  • 1
    Please stop asking people for their email addresses. Nobody on here is your personal helper. –  Jul 12 '12 at 07:20

7 Answers7

2

You can't do that. 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 working thread gets over check the flag value and show the 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 then you can use Handler or runOnUiThread()

Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
1

Try this

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);
Ashwin Singh
  • 7,197
  • 4
  • 36
  • 55
0

make sure you pass right context, for example:

Toast.makeText(MyActivity.this , "error  server not responding " + e.getMessage(),
                Toast.LENGTH_SHORT).show();
Somy A
  • 1,682
  • 15
  • 18
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(...);
});
dennisdrew
  • 4,399
  • 1
  • 25
  • 25
0

Declare globally write it in oncreate and only show 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();
            }
Gundu Bandgar
  • 2,593
  • 1
  • 17
  • 21
  • is not working, here is error : **Can't toast on a thread that has not called Looper.prepare()** – clauub Jun 28 '18 at 11:30
0

This method is working for me if someone still need help:

 getActivity().runOnUiThread(Runnable { Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show() })
clauub
  • 1,134
  • 9
  • 19
-1

check this its working fine 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();
         }
    }
}
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
Vishwanath.M
  • 6,235
  • 11
  • 42
  • 56
  • no it's not, please correct your answer here is why : **Can't toast on a thread that has not called Looper.prepare()** – clauub Jun 28 '18 at 11:29