edittext为空时崩溃

uinbv5nw  于 2021-07-05  发布在  Java
关注(0)|答案(5)|浏览(411)

所以我有一个edittext字段,当用户点击一个数字时,它开始倒计时。当用户没有输入数字时,问题就开始了,应用程序崩溃了。
我试着这么做:
谢谢你的帮助。

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();
        }

    }
jum4pzuy

jum4pzuy1#

我猜问题出在这一行:

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

这是因为,一个空字符串( "" )不能转换为与字符串不同的数字,如 "1" 或者 "2" 要解决这个问题:我们为 NumberFormatException ```
int userNumb = 0;
try {
if (mEnterNumber != null && !TextUtils.isEmpty(mEnterNumber.getText().toString())) {
userNumb = Integer.parseInt(mEnterNumber.getText().toString()) * 60000;
}
}
catch (NumberFormatException ex) {}

ndasle7k

ndasle7k2#

--

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)

 
错误行:

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

vs91vp4v3#

--这是我在安装应用程序时遇到的错误crash:

9w11ddsr

9w11ddsr4#

就像felix favor说的,这是因为你试图把一个非数字项解析成一个字符串。这条线:

mEnterNumber = findViewById(R.id.mEnterNumber);

如果未输入任何内容,则很可能解析为null。您可以尝试将其设置为数值:

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

当然,为了检查或防止其他非数字输入,这总是需要更多的改进。
或者,也可以在xml中这样设置:

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

bz4sfanl5#

在检查文本是否为空之前,无法从edittext获取文本。试试这个:

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

如您所见,我将整数转换放在if块中,因为这可能也是它的原因。

相关问题