There are 2 activity mainactivity and register. by pressing the button, toast is called, but the application crashes, does not produce any errors in consoles.

Toast toast = Toast.makeText(register.this, "11", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); 

What am I doing wrong?

Full code:

 public void onClick(View view) throws MalformedURLException { EditText nickname = (EditText)findViewById(R.id.editText3); if (nickname.getText().toString().equals("")) { AlertDialog.Builder builder1 = new AlertDialog.Builder(this); builder1.setMessage("The login field should not be empty"); builder1.setCancelable(true); builder1.setPositiveButton( "Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert11 = builder1.create(); alert11.show(); } else { String hash = md5(nickname.getText().toString()); // Generate key pair for 4096-bit RSA encryption and decryption Key publicKey = null; Key privateKey = null; try { KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(4096); KeyPair kp = kpg.genKeyPair(); publicKey = kp.getPublic(); privateKey = kp.getPrivate(); System.out.println("public: \n"); System.out.println(publicKey.toString()); System.out.println("private: \n"); System.out.println(privateKey.toString()); } catch (Exception e) { Log.e("Crypto", "RSA key pair error"); } SaveKey(publicKey, hash + "_public_key"); SaveKey(privateKey, hash + "_private_key"); int i=0; //String myURL = "http://s92640jz.bget.ru/register.php"; //String parammetrs = "login=22&open_key=XXX&key_size=4096"; OkHttpClient client = new OkHttpClient(); RequestBody formBody = new FormBody.Builder() .add("login", nickname.getText().toString()) .add("open_key", "XXX") .add("key_size", "4096") .build(); Request request = new Request.Builder() .url("http://s92640jz.bget.ru/register.php") .post(formBody) .build(); client.newCall(request) .enqueue(new Callback() { @Override public void onFailure(final Call call, IOException e) { // Error runOnUiThread(new Runnable() { @Override public void run() { // For the example, you can show an error dialog or a toast // on the main UI thread } }); } @Override public void onResponse(Call call, final Response response) throws IOException { String res = response.body().string(); try{ JSONObject jsonObject = new JSONObject(res); String answer = jsonObject.getString("r_1"); Log.d("TAG", "response is: " + answer); } catch (JSONException e){} try{ JSONObject jsonObject = new JSONObject(res); String answer = jsonObject.getString("r_0"); Log.d("TAG", "response is: " + answer); } catch (JSONException e){} Toast toast = Toast.makeText(register.this, "11", Toast.LENGTH_LONG); //toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); // Do something with the response } }); } 
  • show the button click handling code .. - ZigZag
  • I do not believe that there are no errors in the console. - Vladyslav Matviienko
  • Added to the first post - Nikola Krivosheya
  • The onResponse method does not have access to the UI stream, call Toast from the outside or using Runnable - Ruslan Kucherenko

1 answer 1

onResponce is not called in the UI stream. Toast can be shown (as well as generally manipulated with markup) can only be in the UI stream. Those. you need to surround this code in

  runOnUiThread(new Runnable() { @Override public void run() { //это будет вызвано в UI потоке. Тут можно показать Toast } });