简体   繁体   English

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

[英]Android Studio: Try-catch exception crashes application

I was testing some vulnerabilities inside of my code and trying to fix them by throwing an exception when the user has an invalid input.我正在测试我的代码中的一些漏洞,并试图通过在用户输入无效时抛出异常来修复它们。 Now when I implement a try-catch and run the application on my phone, it crashes when I put in that invalid input.现在,当我实现 try-catch 并在我的手机上运行该应用程序时,当我输入无效输入时它会崩溃。

I assume my code doesn't catch the exception from the addData method.我假设我的代码没有从 addData 方法中捕获异常。 Is there another way to implement exceptions or how can I make it possible to catch the exception from the addData method?是否有另一种方法来实现异常,或者如何才能从 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 Process: com.odisee.photoboothapp, PID: 30232 java.lang.IllegalArgumentException: SQL Exceptie! 05-01 17:58:17.021 30232-30232/com.odisee.photoboothapp E/AndroidRuntime:致命异常:主进程:com.odisee.photoboothapp,PID:30232 java.lang.IllegalArgumentException:SQL 例外! at com.odisee.photoboothapp.Form_Database$1.onClick(Form_Database.java:55) at android.view.View.performClick(View.java:5697) at android.widget.TextView.performClick(TextView.java:10826) at android.view.View$PerformClick.run(View.java:22526) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7224) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)在 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) at android.app.ActivityThread.main(ActivityThread.java:7224) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$ MethodAndArgsCaller.run(ZygoteInit.java:1230) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Try making the catch more generic so you can actually catch the error, then print the stack trace so you can see the issue:尝试使捕获更通用,以便您可以实际捕获错误,然后打印堆栈跟踪,以便您可以看到问题:

try{
    //Try to do something on here
}catch(Exception error1) {
    Log.e(TAG, "The exception caught while executing the process. (error1)")
    error1.printStackTrace();
}

The problem is, you're not wrapping your exception prone code in the try-catch clause.问题是,您没有将易发生异常的代码包装在try-catch子句中。 You've set an OnClickListener to a Button and this is the only part that's being tested.您已将OnClickListener设置为Button ,这是唯一正在测试的部分。

The actual button presses are asynchronous, and they don't take place in the tested block of code.实际的按钮按下是异步的,它们不会发生在经过测试的代码块中。

Actually you don't need an exception here - exceptions should not be used to control normal application flow.实际上,这里不需要异常 - 不应使用异常来控制正常的应用程序流程。 In your case all you should do is to display a Toast that explains to a user that they have used a forbidden keyword.在您的情况下,您应该做的就是显示一个Toast ,向用户解释他们使用了禁止的关键字。

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

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