Java Android表单验证-如何防止空字段在按下按钮时使我的应用程序崩溃?

Java Android表单验证-如何防止空字段在按下按钮时使我的应用程序崩溃?,java,android,arraylist,android-edittext,android-button,Java,Android,Arraylist,Android Edittext,Android Button,我有一个表单,允许用户将条目添加到抽奖中。但是,如果用户将其中一个字段留空并按下提交按钮,应用程序将崩溃。我试图通过创建一个if语句来解决这个问题——如果任何字段为null,则什么也不做。否则,运行意图。然而,这仍然不能解决我的问题。按下按钮仍会导致碰撞 这是我的密码: public void addEntrySubmitButtonClick(View view) { Intent addEntryIntent = getIntent(); int cur

我有一个表单,允许用户将条目添加到抽奖中。但是,如果用户将其中一个字段留空并按下提交按钮,应用程序将崩溃。我试图通过创建一个if语句来解决这个问题——如果任何字段为null,则什么也不做。否则,运行意图。然而,这仍然不能解决我的问题。按下按钮仍会导致碰撞

这是我的密码:

 public void addEntrySubmitButtonClick(View view) {


        Intent addEntryIntent = getIntent();
        int currentRaffleID = addEntryIntent.getIntExtra("raffleIndexInList", 0);
        Raffle currentRaffle = Raffle.raffleArrayList.get(currentRaffleID);
        String newEntryForename = String.valueOf(addEntryForename.getText());
        String newEntrySurname = String.valueOf(addEntrySurname.getText());
        int newEntryTelephoneNo = Integer.parseInt(String.valueOf(addEntryTelephoneNo.getText()));
        int newEntryTicketCount = Integer.parseInt(String.valueOf(addEntryTicketCount.getText()));
        int newEntryRaffleId = currentRaffle.getId();

        if ((newEntryForename.equals(null)) || (newEntrySurname.equals(null)) || (String.valueOf(addEntryTelephoneNo).equals(null)) || (String.valueOf(addEntryTicketCount).equals(null))){
        Intent failIntent = new Intent();
        } else {
            Entry newEntry = new Entry(newEntryForename, newEntrySurname, newEntryTelephoneNo, newEntryTicketCount, newEntryRaffleId);


            // Get the list of raffles
            for(Raffle currentEntryRaffle : Raffle.raffleArrayList) {
                if((currentEntryRaffle.getId() == newEntryRaffleId) && ((currentEntryRaffle.getEntryArrayList().size()) < (currentEntryRaffle.getMaxTickets()))) {
                    int counter=0;
                    do {
                        currentEntryRaffle.getEntryArrayList().add(newEntry);
                        counter++;
                    } while(counter < newEntryTicketCount);
                }
            }

            Intent returnIntent = new Intent();
            setResult(RESULT_CANCELED, returnIntent);
            finish();
        }
上述表达式的计算结果要么为
false
,要么抛出
NullPointerException
。在
null
引用上调用方法是不可能的

String.valueOf(addEntryTelephoneNo).equals(null)
上述表达式的计算结果总是
false
。从
String.valueOf
javadoc:

返回:如果参数为null,则返回一个等于“null”的字符串;否则,将返回obj.toString()的值

你想要的只是

addEntryTelephoneNo == null
其他情况也类似

此外,请注意,您正在尝试在检查空值之前解析该值:

int newEntryTelephoneNo = Integer.parseInt(String.valueOf(addEntryTelephoneNo.getText()));

在空检查后将代码移动到。

我是从智能手机上写的,所以我不会在回答中包含任何代码样本

无论如何。。。首先,删除所有String.valueOf()调用-它们是冗余的。其次,要查看值是否为null,请检查value==null,而不是value.equals(null)。如果值为null,则它没有方法equals(),因此将导致NullReferenceException

另外,在确保Integer.parseInt(value)值不为null之前,不要调用该值。


}

要检查字符串是否为空(null或长度为0),请使用
TextUtils.isEmpty()
。下面是一个例子:

int newEntryTelephoneNo;
if (TextUtils.isEmpty(addEntryTelephoneNo.getText())) {
    newEntryTelephoneNo = 0;
} else {
    newEntryTelephoneNo = Integer.parseInt(addEntryTelephoneNo.getText());
}

logcat中的问题是:

原因:java.lang.NumberFormatException:无效int:“

如果没有行号,我推断问题来自这样一行:

int newEntryTelephoneNo = Integer.parseInt(String.valueOf(addEntryTelephoneNo.getText()));
阅读以下文档给我们一个提示:

投掷

如果字符串不能解析为整数值,则出现NumberFormatException

Integer.parseInt()
似乎无法将空字符串解析为
int
(这是正常的,因为
int
不能为
null

所以我觉得你应该这样做:

int newEntryTelephoneNo = 0;
if (!TextUtils.isEmpty(addEntryTelephoneNo.getText())) {
    newEntryTelephoneNo = Integer.parseInt(addEntryTelephoneNo.getText());
}

与主要问题无关

此外,正如其他答案和评论所说,您必须使用此选项检查对象的“空值”:

myObject == null
而不是

myObject.equals(null)

因为您甚至不能在
null
实例上调用
equals()
或任何其他方法。

当调用
Integer.parseInt
抛出一个
NumberFormatException
时,logcat清楚地显示异常发生。当字符串无法转换为int时会发生这种情况。如果检查行号,您将确切看到是哪个转换生成了异常


根据您的需要,您可能希望同时测试可接受的无效条目,如空字符串和catch
NumberFormatException
,因为输入无效,因此您可以通知用户无效条目。

以下是您需要的代码:

public void addEntrySubmitButtonClick(View view) {


    Intent addEntryIntent = getIntent();
    int currentRaffleID = addEntryIntent.getIntExtra("raffleIndexInList", 0);
    Raffle currentRaffle = Raffle.raffleArrayList.get(currentRaffleID);
    String newEntryForename = String.valueOf(addEntryForename.getText());
    String newEntrySurname = String.valueOf(addEntrySurname.getText());
    int newEntryTelephoneNo = Integer.parseInt(String.valueOf(addEntryTelephoneNo.getText()));
    int newEntryTicketCount = Integer.parseInt(String.valueOf(addEntryTicketCount.getText()));
    int newEntryRaffleId = currentRaffle.getId();

    if (newEntryForename==null || newEntrySurname == null || String.valueOf(addEntryTelephoneNo) == null || String.valueOf(addEntryTicketCount) == null){
    Intent failIntent = new Intent();
    } else {
        Entry newEntry = new Entry(newEntryForename, newEntrySurname, newEntryTelephoneNo, newEntryTicketCount, newEntryRaffleId);


        // Get the list of raffles
        for(Raffle currentEntryRaffle : Raffle.raffleArrayList) {
            if((currentEntryRaffle.getId() == newEntryRaffleId) && ((currentEntryRaffle.getEntryArrayList().size()) < (currentEntryRaffle.getMaxTickets()))) {
                int counter=0;
                do {
                    currentEntryRaffle.getEntryArrayList().add(newEntry);
                    counter++;
                } while(counter < newEntryTicketCount);
            }
        }

        Intent returnIntent = new Intent();
        setResult(RESULT_CANCELED, returnIntent);
        finish();
    }
public void addentrysubmitbutton单击(视图){
Intent addEntryIntent=getIntent();
int currentlaffleid=addEntryIntent.getIntExtra(“raffleIndexInList”,0);
抽奖券currentRaffle=Raffle.raffleArrayList.get(currentRaffleID);
String newEntryForename=String.valueOf(addEntryForename.getText());
String NewEntryNames=String.valueOf(addEntrySurname.getText());
int newEntryTelephoneNo=Integer.parseInt(String.valueOf(addEntryTelephoneNo.getText());
int newEntryTicketCount=Integer.parseInt(String.valueOf(addEntryTicketCount.getText());
int newEntryRaffleId=currentRaffle.getId();
if(newEntryForename==null | | newentryname==null | | String.valueOf(addEntryTelephoneNo)==null | | String.valueOf(addEntryTicketCount)==null){
意向失效意向=新意向();
}否则{
Entry newEntry=新条目(newEntryForename、newentryname、newEntryTelephoneNo、newEntryTicketCount、newEntryRaffleId);
//得到莱佛士的名单
用于(抽奖活动当前入口抽奖活动:Raffle.raffleArrayList){
如果((currentEntryRaffle.getId()==newEntryRaffleId)和((currentEntryRaffle.getEntryArrayList().size())<(currentEntryRaffle.getMaxTickets())){
int计数器=0;
做{
currentEntryRaffle.getEntryArrayList().add(newEntry);
计数器++;
}while(计数器

您必须替换.equals(null)with==null,因为在null指针上调用一个方法会使你的应用程序崩溃。

请提供logcat给我们好吗?不要使用
equals
进行null检查!使用
==
。不要使用Java的JavaScript/HTML代码段功能!我已经从你的问题中删除了它,现在又回来了。谢谢你的提示。不过,已经更改了它仍在崩溃。对于html的事情,很抱歉。我在这里使用String.valueOf的原因是因为我不能使用int==null
myObject == null
myObject.equals(null)
public void addEntrySubmitButtonClick(View view) {


    Intent addEntryIntent = getIntent();
    int currentRaffleID = addEntryIntent.getIntExtra("raffleIndexInList", 0);
    Raffle currentRaffle = Raffle.raffleArrayList.get(currentRaffleID);
    String newEntryForename = String.valueOf(addEntryForename.getText());
    String newEntrySurname = String.valueOf(addEntrySurname.getText());
    int newEntryTelephoneNo = Integer.parseInt(String.valueOf(addEntryTelephoneNo.getText()));
    int newEntryTicketCount = Integer.parseInt(String.valueOf(addEntryTicketCount.getText()));
    int newEntryRaffleId = currentRaffle.getId();

    if (newEntryForename==null || newEntrySurname == null || String.valueOf(addEntryTelephoneNo) == null || String.valueOf(addEntryTicketCount) == null){
    Intent failIntent = new Intent();
    } else {
        Entry newEntry = new Entry(newEntryForename, newEntrySurname, newEntryTelephoneNo, newEntryTicketCount, newEntryRaffleId);


        // Get the list of raffles
        for(Raffle currentEntryRaffle : Raffle.raffleArrayList) {
            if((currentEntryRaffle.getId() == newEntryRaffleId) && ((currentEntryRaffle.getEntryArrayList().size()) < (currentEntryRaffle.getMaxTickets()))) {
                int counter=0;
                do {
                    currentEntryRaffle.getEntryArrayList().add(newEntry);
                    counter++;
                } while(counter < newEntryTicketCount);
            }
        }

        Intent returnIntent = new Intent();
        setResult(RESULT_CANCELED, returnIntent);
        finish();
    }