我有一段非常容易出错的代码,所以我用try{}catch语句包装了它。我不想回到上一个活动,我只想它停留在当前活动上,这样用户就可以编辑它们的任何错误。
我该如何实现?
try{
orgi.insertOrThrow(tableName, null, values);
Toast.makeText(this, "You have successfully created a new profile!", 2).show();
gotoProfileList();
Log.e(getClass().getSimpleName(),"Successfully added to database");
}catch(SQLiteException se){
Log.e(getClass().getSimpleName(), "Database connection failed!" + se);
//Stay in this activity...
}finally{
if (orgi != null){
orgi.close();
}
}
算了吧,我可以通过显示一个alertDialog来解决我自己的问题,该对话框会告诉用户有关错误的信息。无论如何谢谢。:)
try{
orgi.insertOrThrow(tableName, null, values);
Toast.makeText(this, "You have successfully created a new profile!", 2).show();
gotoProfileList();
Log.e(getClass().getSimpleName(),"Successfully added to database");
}catch(SQLiteException se){
Log.e(getClass().getSimpleName(), "Database connection failed!" + se);
displayError();
//stayInThisActivity();
}finally{
if (orgi != null){
}
public void displayError(){
AlertDialog.Builder error = new AlertDialog.Builder(this);
error.setMessage("That profile name already exists, try another one.").setCancelable(false).setPositiveButton("Yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = error.create();
alert.setTitle("Error");
alert.show();
}