简体   繁体   English

如何敬酒异常消息

[英]How to Toast the Exception message

I am creating one android application in which i am trying to insert the values in sqlite database but When i try to insert wrong value it display in terminal android.database.sqlite.SQLiteConstraintException: CHECK constraint failed: but i want to toast one message .我正在创建一个 android 应用程序,在其中我试图在 sqlite 数据库中插入值,但是当我尝试插入错误的值时,它显示在终端android.database.sqlite.SQLiteConstraintException: CHECK constraint failed: but i want toast a message 。 I have used try-catch block but it doesn't working.我使用了 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();
                     }

                 }

            }
        });

This exception getting in terminal but I want to catch that exception in try catch block:-此异常进入终端,但我想在 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)

if you wanna show the exception message on the toast, replace:如果您想在 toast 上显示异常消息,请替换:

Toast.makeText(getApplicationContext(), "please select blood group", Toast.LENGTH_SHORT).show();

with:和:

Toast.makeText(getApplicationContext(), e.getMessage() , Toast.LENGTH_SHORT).show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM