Pregunta

Ok

try {

            Log.d("request!", "starting");
            // getting product details by making HTTP request
            JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
                    params);

            // check your log for json response
            Log.d("Login attempt", json.toString());

            }
        } catch (JSONException e) {

//ERROR WHEN INERTING CODE HERE!.

}<code>

Hi guys!, I just want to know why my app crashes when I write code in the indicated place (see above).. If I leave it blank, nothing happens...

The thing is, I want to show a

Toast.makeText(MainActivity.this, "Couldn't reach the server", Toast.LENGTH_LONG).show();

Why do you think that is?. Thanks in advance

¿Fue útil?

Solución 2

I'm assuming because you're error is

"Couldn't reach the server"

you are trying to make network calls, which means your code is in a thread of some sort? You cannot touch UI elements from inside the main thread so move:

Toast.makeText(MainActivity.this, "Couldn't reach the server", Toast.LENGTH_LONG).show();

Into the postExecute() method of an AsyncTask or use a Handler

From the docs

Do not access the Android UI toolkit from outside the UI thread.

Otros consejos

Are you trying to access UI elements from outside the main thread by any chance? Try using Log.v instead of Toast and see if that helps.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top