It is given: a dialog box, when clicked, an error appears android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed (what kind of error it is and why it appears I know)
Necessary: catch this exception and display a message (another Record already exists! Dialog box)
Question: how to implement it? As far as I understand, the catch does not work, because in the логе does not show my message
Important: so that when you click OK, the first dialog box does not close if an error occurs.
private void showAddArticleDialog() { AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); LayoutInflater inflater = this.getLayoutInflater(); final View dialogView = inflater.inflate(R.layout.add_article_dialog, null); dialogBuilder.setView(dialogView); final EditText edt = (EditText) dialogView.findViewById(R.id.edit1); String title = ""; //String message = "Enter text below"; if(TYPE){ title = getString(R.string.title_add_income_article); }else{ title = getString(R.string.title_add_cost_article); } dialogBuilder.setTitle(title); //dialogBuilder.setMessage(message); dialogBuilder.setPositiveButton(getString(R.string.ok_button), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { try { myDb.addCategory(edt.getText().toString(), R.drawable.star, TYPE); } catch (SQLException e) { e.printStackTrace(); //showLog(getString(R.string.this_article_exists)); Log.e("ERROR", "ERROR IN CODE: " + e.toString() + " ------- " + R.string.this_article_exists); } initializeAdapter(); } }); dialogBuilder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //pass } }); AlertDialog b = dialogBuilder.create(); b.show(); } Here is the log:
02-28 12:28:04.891 23792-23792/ru.by_em.my_finances E/SQLiteLog: (2067) abort at 17 in [INSERT INTO direction(category_name,category_photo,_id) VALUES (?,?,?)]: UNIQUE constraint failed: direction.category_name 02-28 12:28:04.896 23792-23792/ru.by_em.my_finances E/SQLiteDatabase: Error inserting category_name=Обеды category_photo=2130837652 _id=13 android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: direction.category_name (code 2067) at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method) at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:952) at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788) at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86) at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1595) at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1465) at ru.by_em.my_finances.db.DBDataSource.addCategory(DBDataSource.java:241) at ru.by_em.my_finances.activities.ChangeArticlesActivity$1.onClick(ChangeArticlesActivity.java:100) at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5951) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)