Android Studio:Try-catch异常崩溃了应用程序

时间:2017-05-01 15:29:04

标签: java android exception-handling

我正在测试代码中的一些漏洞,并尝试通过在用户输入无效时抛出异常来修复它们。现在当我实现try-catch并在我的手机上运行应用程序时,当我输入无效输入时它会崩溃。

我认为我的代码没有从addData方法中捕获异常。是否有另一种方法来实现异常,或者如何才能从addData方法中捕获异常?

package com.odisee.photoboothapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.odisee.photoboothapp.fontchanger.FontChangeTextView;

public class Form_Database extends AppCompatActivity {
DatabaseHelper myDb;

int selectedId;
RadioGroup test;
RadioButton editEducation;
EditText editName, editSurname, editEmail;
Button btnAddData;

@Override
protected void onCreate(Bundle savedInstanceState) throws IllegalArgumentException {
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_form__database);

    myDb = new DatabaseHelper(this);

    test = (RadioGroup)findViewById(R.id.radioButtonChoice);

    editName = (EditText)findViewById(R.id.edit_Name);
    editSurname = (EditText)findViewById(R.id.edit_Surname);
    editEmail = (EditText)findViewById(R.id.edit_Email);


    btnAddData = (Button)findViewById(R.id.btnSend);
    try {
        addData();
    }
    catch(IllegalArgumentException e) {
        Toast.makeText(Form_Database.this,"Data not inserted" + e.getMessage(),Toast.LENGTH_LONG).show();
    }
}

public void addData() {
    btnAddData.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        if(editName.getText().toString().contains("DROP")) {
                            throw new IllegalArgumentException("SQL Exceptie!");
                        }
                        else {

                            selectedId = test.getCheckedRadioButtonId();
                            editEducation = (RadioButton)findViewById(selectedId);
                            boolean isInserted = myDb.insertData(editName.getText().toString(), editSurname.getText().toString(), editEmail.getText().toString(), editEducation.getText().toString());

                            sendEmail();

                            if(isInserted == true) {
                                Toast.makeText(Form_Database.this,"Data inserted",Toast.LENGTH_LONG).show();
                            }
                            else {
                                Toast.makeText(Form_Database.this,"Data not inserted",Toast.LENGTH_LONG).show();
                            }
                        }
                }
            }
    );
}

public void sendEmail() {
    //Getting content for email
    String email = editEmail.getText().toString();
    String subject = "testberichtje voor lorenzo";
    String message = "testberichtje voor lorenzo";

    //Creating SendMail object
    SendMail sm = new SendMail(this, email, subject, message);

    //Executing sendmail to send email
    sm.execute();
}

}
  

05-01 17:58:17.021 30232-30232 / com.odisee.photoboothapp E / AndroidRuntime:FATAL EXCEPTION:main                                                                             过程:com.odisee.photoboothapp,PID:30232                                                                             java.lang.IllegalArgumentException:SQL Exceptie!                                                                                 在com.odisee.photoboothapp.Form_Database $ 1.onClick(Form_Database.java:55)                                                                                 在android.view.View.performClick(View.java:5697)                                                                                 在android.widget.TextView.performClick(TextView.java:10826)                                                                                 在android.view.View $ PerformClick.run(View.java:22526)                                                                                 在android.os.Handler.handleCallback(Handler.java:739)                                                                                 在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                 在android.os.Looper.loop(Looper.java:158)                                                                                 在android.app.ActivityThread.main(ActivityThread.java:7224)                                                                                 at java.lang.reflect.Method.invoke(Native Method)                                                                                 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)                                                                                 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

2 个答案:

答案 0 :(得分:2)

问题是,您没有在try-catch子句中包含容易出错的代码。您已将OnClickListener设置为Button,这是唯一正在测试的部分。

按下的实际按钮是异步的,它们不会在测试的代码块中发生。

实际上这里不需要例外 - 不应该使用例外来控制正常的应用程序流。在您的情况下,您应该只显示一个Toast,向用户解释他们使用了禁用的关键字。

答案 1 :(得分:1)

尝试使catch更通用,以便您可以实际捕获错误,然后打印堆栈跟踪以便您可以看到问题:

catch(Exception ex) {
    Log.e(TAG, "EXCEPTION CAUGHT WHILE EXECUTING DATABASE TRANSACTION")
    ex.printStackTrace();
}