Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying a custom AlertDialog in Android while catching an Exception
    text
    copied!<p>I'm trying to display an alert message for the users in a catch block. The difference is that my try/catch is inside the <code>onCreate()</code> from the main activity, so it's executed as soon as the application is opened.</p> <p>I've tried this (I have an <code>OnClick()</code> for Dialogs in the end of the Activity with <code>dialog.dismiss()</code> and <code>this.finish()</code> after):</p> <pre><code> catch (SQLException e) { AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle(getString(R.string.label_title_error)) .setIcon(R.drawable.error_icon) .setMessage(getString(R.string.msg_error_sql)) .setPositiveButton(getString(R.string.label_ok), this); AlertDialog dialogSQL = builder.create(); dialogSQL.show(); e.printStackTrace(); } </code></pre> <p>and I also tried this:</p> <pre><code> catch (NumberFormatException e) { AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle(getString(R.string.label_title_error)); dialog.setIcon(R.drawable.error_icon); dialog.setMessage(getString(R.string.msg_error_numberformat)); dialog.setNeutralButton(getString(R.string.label_ok), null); dialog.create().show(); e.printStackTrace(); } </code></pre> <p>Forced the exceptions while debugging and I can see that it simply catches the exception, displays in the LogCat (as warning) and keep with the flow until it hits another untreated exception and then display that default "Sorry!/Force Close" Android dialog. And there are no other exceptions related to the dialog in the LogCat.</p> <p>Why it does not display my custom AlertDialogs for the catch? I thought about the context that the Builder needs, but if the <code>super()</code> from <code>onCreate()</code> is before this code so why it does displays the message?</p> <p>Thanks Everyone.</p> <p>UPDATE: Ok, as requested, here goes more code.</p> <pre><code>public class PinActivity extends Activity implements OnClickListener, android.content.DialogInterface.OnClickListener{ private Facade facade = null; public static int INSERT_SAL = 0; public static int INSERT_OK = 1; public static int INSERT_CANCEL = 2; EditText edtIniPin; TextView txtSelecPin; TextView txtCancPin; int pinMaxLengthInt; private String[] serviceNames; @Override protected void onCreate(Bundle savedInstanceState) { Log.v("FLUXO", "PIN --&gt;&gt; ON CREATE"); super.onCreate(savedInstanceState); facade = Facade.getInstance(); try { serviceNames = facade.loadApplication(this); facade.loadParameters(0); setContentView(R.layout.pin_screen); //Instanciando Views da Tela edtIniPin = (EditText) findViewById(R.id.editTextPin); txtSelecPin = (TextView) findViewById(R.id.btn_select_pin); txtCancPin = (TextView) findViewById(R.id.btn_cancel_pin); pinMaxLengthInt = Facade.getInstance().getPinSize(); InputFilter[] FilterArray = new InputFilter[1]; FilterArray[0] = new InputFilter.LengthFilter(pinMaxLengthInt); edtIniPin.setFilters(FilterArray); edtIniPin.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (edtIniPin.getText().length() &gt; 0) { txtCancPin.setText(getString(R.string.btn_clean)); } else if (edtIniPin.getText().length() == 0){ txtCancPin.setText(getString(R.string.btn_exit)); } else if (edtIniPin.getText().length() == Facade.getInstance().getPinSize()){ edtIniPin.setEnabled(false); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }); } catch (SQLException e) { AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle(getString(R.string.label_title_error)) .setIcon(R.drawable.error_icon) .setMessage(getString(R.string.msg_error_sql)) .setPositiveButton(getString(R.string.label_ok), this); AlertDialog dialogSQL = builder.create(); dialogSQL.show(); e.printStackTrace(); } catch (CorruptedAppException e) { AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle(getString(R.string.label_title_error)) .setIcon(R.drawable.error_icon) .setMessage(getString(R.string.msg_error_corrupted)) .setPositiveButton(getString(R.string.label_ok), this); AlertDialog dialogCorrupted = builder.create(); dialogCorrupted.show(); e.printStackTrace(); } catch (NoServiceAvailableException e) { AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle(getString(R.string.label_title_error)) .setIcon(R.drawable.error_icon) .setMessage(getString(R.string.msg_error_noservice)) .setPositiveButton(getString(R.string.label_ok), this); AlertDialog dialogNoService = builder.create(); dialogNoService.show(); e.printStackTrace(); } catch (NumberFormatException e) { AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle(getString(R.string.label_title_error)); dialog.setIcon(R.drawable.error_icon); dialog.setMessage(getString(R.string.msg_error_numberformat)); dialog.setNeutralButton(getString(R.string.label_ok), null); dialog.create().show(); e.printStackTrace(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.v("FLUXO", "PIN --&gt;&gt; ON ACTIVITY RESULT"); if (resultCode == INSERT_OK) { String[] initCode = (String[]) data.getSerializableExtra("init_code"); try { Facade.getInstance().insertInitCode(serviceNames[0], initCode, this); } catch (NumberFormatException e) { AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle(getString(R.string.label_title_error)) .setIcon(R.drawable.error_icon) .setMessage(getString(R.string.msg_error_numberformat)) .setPositiveButton(getString(R.string.label_ok), this); AlertDialog dialog = builder.create(); dialog.show(); e.printStackTrace(); } catch (SQLException e) { AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle(getString(R.string.label_title_error)) .setIcon(R.drawable.error_icon) .setMessage(getString(R.string.msg_error_sql)) .setPositiveButton(getString(R.string.label_ok), this); AlertDialog dialog = builder.create(); dialog.show(); e.printStackTrace(); } catch (CorruptedAppException e) { AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle(getString(R.string.label_title_error)) .setIcon(R.drawable.error_icon) .setMessage(getString(R.string.msg_error_corrupted)) .setPositiveButton(getString(R.string.label_ok), this); AlertDialog dialog = builder.create(); dialog.show(); e.printStackTrace(); } } else if (resultCode == INSERT_CANCEL){ this.finish(); } } @Override protected void onStart() { Log.v("FLUXO", "PIN --&gt;&gt; ON START"); super.onStart(); txtSelecPin.setOnClickListener(this); txtCancPin.setOnClickListener(this); } @Override protected void onResume() { Log.v("FLUXO", "PIN --&gt;&gt; ON RESUME"); super.onResume(); if (!facade.isInitialized(serviceNames[0], this)) { Intent itInicial = new Intent(this, InitialActivity.class); startActivityForResult(itInicial, INSERT_SAL); } } @Override protected void onStop() { Log.v("FLUXO", "PIN --&gt;&gt; ON STOP"); super.onStop(); } @Override protected void onPause() { Log.v("FLUXO", "PIN --&gt;&gt; ON PAUSE"); super.onPause(); } @Override public void onBackPressed() { Log.v("FLUXO", "PIN --&gt;&gt; ON BACK KEY PRESSED"); super.onBackPressed(); moveTaskToBack(true); } @Override protected void onDestroy() { Log.v("FLUXO", "PIN --&gt;&gt; ON DESTROY"); super.onDestroy(); } /** * Método chama próxima tela */ private void nextScreen(){ Log.v("FLUXO", "PIN --&gt;&gt; NEXT SCREEN"); Intent it = new Intent(this, ValueActivity.class); startActivity(it); } @Override public void onClick(View v) { //lots of stuff (...) } @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); System.exit(0); } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload