有什么办法可以防止我的应用崩溃(Android和数据库实践)

时间:2019-07-07 23:52:44

标签: java android sqlite crash

builder.setView(v);         builder.setPositiveButton(“ Submit”,新的DialogInterface.OnClickListener(){

        @Override
        public void onClick(DialogInterface dialog, int which) {
            EditText edituser ;
            edituser = (EditText) findViewById(R.id.edit_username);

            Database myDB;
            myDB = new Database(MainActivity.this);
            boolean isInserted = myDB.DataInput(edituser.getText().toString());
            if(isInserted == true)
                Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
            else
                Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
            Dialog();
        }
    })
    .setNegativeButton("Cancel",null)
    .setCancelable(false);

1 个答案:

答案 0 :(得分:-1)

使用trycatch

builder.setView (v); builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
           try{
                EditText edituser ;
            edituser = (EditText) findViewById(R.id.edit_username);

            Database myDB;
            myDB = new Database(MainActivity.this);
            boolean isInserted = myDB.DataInput(edituser.getText().toString());
            if(isInserted == true)
                Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
            else
                Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
            Dialog();
           }catch (Exception ex) {e.printStackTrace();}
        }
    })
    .setNegativeButton("Cancel",null)
    .setCancelable(false);