“EditText”在为空时崩溃。

huangapple go评论60阅读模式
英文:

EditText crash when empty

问题

这是您提供的代码的翻译部分:

public class MainActivity extends AppCompatActivity {

    EditText mEnterNumber;
    TextView mTest;
    int timerIsRunning = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void StartRemainder(View view){

        mTest = findViewById(R.id.mTest);
        mEnterNumber = findViewById(R.id.mEnterNumber);

        int userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000;

        if(userNumb < 0 || mEnterNumber.toString().isEmpty()){
            Toast.makeText(MainActivity.this, "Please enter correct number", Toast.LENGTH_SHORT).show();
        }
        else{

        CountDownTimer userTimer = new CountDownTimer(userNumb,1000) {

            @Override
            public void onTick(long millisUntilFinished) {

                long millis = millisUntilFinished;

                //Convert milliseconds into hour,minute and seconds
                String hms = String.format("%02d:%02d:%02d",
                        TimeUnit.MILLISECONDS.toHours(millis),
                        TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                        TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
                mTest.setVisibility(View.VISIBLE);
                mTest.setText(hms);
                timerIsRunning = 1;

            }

            @Override
            public void onFinish() {

            }
        }.start();
        }

    }
}

这是您提供的代码中的错误信息的翻译部分:

2020-09-04 04:09:15.024 27096-27096/com.example.drinkme E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.drinkme, PID: 27096
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NumberFormatException: For input string: ""
at java.lang.Integer.parseInt(Integer.java:620)
at java.lang.Integer.parseInt(Integer.java:643)
at com.example.drinkme.MainActivity.StartRemainder(MainActivity.java:40)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
line of error:
int userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000;

请注意,翻译结果中可能包含特殊字符和排版格式,这可能会影响代码的可读性。

英文:

so I have an EditText field,and when the user tap a number,its start counting down.
The problem starts when the are not entering a number the app crash.

this is what I tried to do:

Thanks for the help.


public class MainActivity extends AppCompatActivity {
EditText mEnterNumber;
TextView mTest;
int timerIsRunning = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void StartRemainder(View view){
mTest = findViewById(R.id.mTest);
mEnterNumber = findViewById(R.id.mEnterNumber);
int userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000;
if(userNumb &lt; 0 || mEnterNumber.toString().isEmpty()){
Toast.makeText(MainActivity.this, &quot;Please enter correct number&quot;, Toast.LENGTH_SHORT).show();
}
else{
CountDownTimer userTimer = new CountDownTimer(userNumb,1000) {
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
//Convert milliseconds into hour,minute and seconds
String hms = String.format(&quot;%02d:%02d:%02d&quot;,
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
mTest.setVisibility(View.VISIBLE);
mTest.setText(hms);
timerIsRunning = 1;
}
@Override
public void onFinish() {
}
}.start();
}
}

----------------------This is the error I get when the app crash:--------------------------------

2020-09-04 04:09:15.024 27096-27096/com.example.drinkme E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.drinkme, PID: 27096
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6256)&#160;
at android.view.View$PerformClick.run(View.java:24701)&#160;
at android.os.Handler.handleCallback(Handler.java:789)&#160;
at android.os.Handler.dispatchMessage(Handler.java:98)&#160;
at android.os.Looper.loop(Looper.java:164)&#160;
at android.app.ActivityThread.main(ActivityThread.java:6541)&#160;
at java.lang.reflect.Method.invoke(Native Method)&#160;
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)&#160;
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)&#160;
Caused by: java.lang.NumberFormatException: For input string: &quot;&quot;
at java.lang.Integer.parseInt(Integer.java:620)
at java.lang.Integer.parseInt(Integer.java:643)
at com.example.drinkme.MainActivity.StartRemainder(MainActivity.java:40)
at java.lang.reflect.Method.invoke(Native Method)&#160;
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)&#160;
at android.view.View.performClick(View.java:6256)&#160;
at android.view.View$PerformClick.run(View.java:24701)&#160;
at android.os.Handler.handleCallback(Handler.java:789)&#160;
at android.os.Handler.dispatchMessage(Handler.java:98)&#160;
at android.os.Looper.loop(Looper.java:164)&#160;
at android.app.ActivityThread.main(ActivityThread.java:6541)&#160;
at java.lang.reflect.Method.invoke(Native Method)&#160;
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)&#160;
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

 

line of error:

int userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000;

答案1

得分: 2

我猜问题出在这一行:

int userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000;

问题在于,一个空字符串(&quot;&quot;)无法像字符串&quot;1&quot;&quot;2&quot;那样转换为数字。

修复方法:
我们为NumberFormatException提供一个try-catch

int userNumb = 0;
try {
    if (mEnterNumber != null && !TextUtils.isEmpty(mEnterNumber.getText().toString())) {
        userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000; 
    }
} 
catch (NumberFormatException ex) {}
英文:

I'm guessing the issue is with this line:

int userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000;

And it is because, an empty string(&quot;&quot;) cannot be converted to a number unlike strings like &quot;1&quot; or &quot;2&quot;

To fix this:
We Provide a try-catch block for the NumberFormatException

int userNumb = 0;
try {
if (mEnterNumber != null &amp;&amp; !TextUtils.isEmpty(mEnterNumber.getText().toString())) {
userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000; 
}
} 
catch (NumberFormatException ex) {}

答案2

得分: 2

你在检查文本框是否为空之前没有从编辑框中获取文本内容。请尝试以下代码:

if(userNumb < 0 && mEnterNumber.getText().toString().isEmpty()){
    Toast.makeText(MainActivity.this, "请输入正确的数字", Toast.LENGTH_SHORT).show();
    int userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000;
}

正如你所看到的,我已经将整数转换放在了if块中,因为这也可能是问题的原因之一。

英文:

You're not getting the text from the edittext before checking weather it's empty or not. Try this :

if(userNumb &lt; 0 &amp;&amp; mEnterNumber.`getText()`.toString().isEmpty()){
Toast.makeText(MainActivity.this, &quot;Please enter correct number&quot;, 
Toast.LENGTH_SHORT).show();
int userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000;
}

As you can see I've put the integer conversion in the if block as it might also be the reason for it.

答案3

得分: 1

像Felix Favour所说的那样,这是因为您正在尝试解析非数字项目为字符串。这一行:

mEnterNumber = findViewById(R.id.mEnterNumber);

如果没有输入任何内容,很可能会解析为空。您可以尝试将其设置为数值:

mEnterNumber = findViewById(R.id.mEnterNumber);
if (mEnterNumber.getText().toString().equals(""))
{
int userNumb = 0;  // 或者任何您想要设置为起始默认值的值
}
else
{
int userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000;
}

当然,这无疑可能需要更多的改进来检查或防止其他非数字的输入。

或者,您也可以在XML中这样设置它:

<EditText
android:id="@+id/mEnterNumber"
...
android:text="0" />
英文:

Like Felix Favour said it's because you're trying to parse a non-numeric item into a string. This line:

mEnterNumber = findViewById(R.id.mEnterNumber);

Will most likely resolve to Null if nothing is entered. You can try setting it to a numeric value:

    mEnterNumber = findViewById(R.id.mEnterNumber);
if mEnterNumber.getText().toString().equals(&quot;&quot;)
{
int userNumb = 0;  // or whatever value you want to set as a starting default
}
else
{
int userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000;
}

This invariably could need more improvement for checking or preventing other non-numeric entries ofcourse.

Alternatively, you can also set it in the XML like this:

    &lt;EditText
android:id=&quot;@+id/mEnterNumber&quot;
...
android:text=&quot;0&quot; /&gt;

答案4

得分: 0

我使用类似这样的代码 -

if (mEnterNumber.getText().toString().matches("")) {
    Toast.makeText(this, "请输入任何数字", Toast.LENGTH_SHORT).show();
    return;
}

我希望这会防止崩溃。

英文:

I use something like this -

if (mEnterNumber.getText().toString().matches(&quot;&quot;)){
Toast.makeText(this, &quot;Please Enter any number&quot;, Toast.LENGTH_SHORT).show();
return;
}

I hope this will prevent the crash.

huangapple
  • 本文由 发表于 2020年9月4日 08:53:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63733473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定