Android : My app crashes when there is a blank editText field

后端 未结 1 1119
天涯浪人
天涯浪人 2021-01-19 18:15

I have a problem with my code. It keeps on crashing when i have a blank editText field. This bit of code is in the settings of my app and works the data fine but when ther

相关标签:
1条回答
  • 2021-01-19 18:30
    if (task1.toString().length() < 0) {
        task1.toString();
        t1.setText(task1);
    }
    else {
        t1.setText(edit);
    }
    

    The above makes no sense at all.

    Firstly task1 IS a string so there's no need to call toString() to convert it to one.

    Secondly, your conditional statement (if) is checking to see if task1 has a length less than zero....think about it.

    Thirdly, if it does have an impossible length less than zero you then call toString() on it again (with no variable to receive the impossible less than zero result) and you then try to set the text of your t1 EditText.

    The chances are that your file reading is failing (probably because you only save the strings later in the onClick(...) method). Because your task strings will be null if the file reading fails then you need to test for null before trying to use them.

    In other words you're doing this in your code...

    String task1 = null;
    

    To fix the bit of code I enclosed at the beginning, use...

    if (task1 != null) {
        t1.setText(task1);
    }
    else {
        t1.setText(edit);
    }
    

    ...but most importantly, make sure your files have the strings in them that you need to read.

    0 讨论(0)
提交回复
热议问题