[英]How to Toast the Exception message
我正在創建一個 android 應用程序,在其中我試圖在 sqlite 數據庫中插入值,但是當我嘗試插入錯誤的值時,它顯示在終端android.database.sqlite.SQLiteConstraintException: CHECK constraint failed:
but i want toast a message 。 我使用了 try-catch 塊,但它不起作用。
registerbtn.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {
if (isAllFieldsChecked = CheckAllFields()) {
try {
dbHandler.insertValue(name.getText().toString(),
Age.getText().toString(), gender1.trim(),
email.getText().toString(), username.getText().toString(),
password.getText().toString(), contactno.getText().toString(),
Address.getText().toString(), city.getText().toString(),
encodedImage, item, uid.getText().toString());
Toast.makeText(getApplicationContext(), "Registration successful.", Toast.LENGTH_LONG).show();
textToSpeech.speak("Registration successful.",TextToSpeech.QUEUE_FLUSH,null);
Intent i = new Intent(RegisterActivity.this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
catch (SQLiteConstraintException e) {
final TextView textView=findViewById(R.id.bloodgroup6);
final ScrollView sc = findViewById(R.id.scroll);
sc.post(new Runnable() {
@Override
public void run() {
sc.scrollTo(0,city.getTop());
}
});
textToSpeech.speak("please select blood group",TextToSpeech.QUEUE_FLUSH,null);
Toast.makeText(getApplicationContext(), "please select blood group", Toast.LENGTH_SHORT).show();
}
}
}
});
此異常進入終端,但我想在 try catch 塊中捕獲該異常:-
E/SQLiteDatabase: Error inserting Uid=334 password=4567 img=null bloodgrp=select blood Email=ffg gender=male Address=gg Username=rushikesh city=ouh age=55 Name=ggg contactNo=6699588855
android.database.sqlite.SQLiteConstraintException: CHECK constraint failed: register (code 275)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:783)
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:1560)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1429)
at com.example.software2.bloodbankmanagement.DBHandler.insertValue(DBHandler.java:107)
at com.example.software2.bloodbankmanagement.RegisterActivity$19.onClick(RegisterActivity.java:384)
如果您想在 toast 上顯示異常消息,請替換:
Toast.makeText(getApplicationContext(), "please select blood group", Toast.LENGTH_SHORT).show();
和:
Toast.makeText(getApplicationContext(), e.getMessage() , Toast.LENGTH_SHORT).show();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.