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 onCreate()
from the main activity, so it's executed as soon as the application is opened.
I've tried this (I have an OnClick()
for Dialogs in the end of the Activity with dialog.dismiss()
and this.finish()
after):
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();
}
and I also tried this:
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();
}
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.
Why it does not display my custom AlertDialogs for the catch? I thought about the context that the Builder needs, but if the super()
from onCreate()
is before this code so why it does displays the message?
Thanks Everyone.
UPDATE: Ok, as requested, here goes more 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 -->> 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() > 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 -->> 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 -->> ON START");
super.onStart();
txtSelecPin.setOnClickListener(this);
txtCancPin.setOnClickListener(this);
}
@Override
protected void onResume() {
Log.v("FLUXO", "PIN -->> 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 -->> ON STOP");
super.onStop();
}
@Override
protected void onPause() {
Log.v("FLUXO", "PIN -->> ON PAUSE");
super.onPause();
}
@Override
public void onBackPressed() {
Log.v("FLUXO", "PIN -->> ON BACK KEY PRESSED");
super.onBackPressed();
moveTaskToBack(true);
}
@Override
protected void onDestroy() {
Log.v("FLUXO", "PIN -->> ON DESTROY");
super.onDestroy();
}
/**
* Método chama próxima tela
*/
private void nextScreen(){
Log.v("FLUXO", "PIN -->> 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);
}
}