gpt4 book ai didi

android - 在捕获异常时在 Android 中显示自定义 AlertDialog

转载 作者:行者123 更新时间:2023-11-30 04:41:33 24 4
gpt4 key购买 nike

我正在尝试在 catch block 中为用户显示警告消息。不同之处在于我的 try/catch 位于主 Activity 的 onCreate() 中,因此它会在应用程序打开后立即执行。

我试过这个(我有一个 OnClick() 用于在 Activity 的末尾使用 dialog.dismiss()this.finish () 之后:

 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 (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();
}

在调试时强制异常,我可以看到它只是捕获异常,显示在 LogCat 中(作为警告)并保持流程直到它遇到另一个未处理的异常,然后显示默认的“抱歉!/强制关闭”安卓对话框。并且没有其他与 LogCat 中的对话框相关的异常。

为什么它不为捕获显示我的自定义 AlertDialogs?我考虑了 Builder 需要的上下文,但如果 onCreate() 中的 super() 在此代码之前,那么为什么它会显示消息?

谢谢大家

更新:好的,按照要求,这里有更多代码。

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);
}

最佳答案

AlertDialog 通常需要其父上下文可见才能正常工作(根据我的经验;如果我错了,请有人纠正我)。除非有其他错误(如果没有完整的代码很难判断),如果将它移到 onResume() 函数中,您可能会使其正常工作。

关于android - 在捕获异常时在 Android 中显示自定义 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5899710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com