gpt4 book ai didi

java - 字符串到 int。无效整数

转载 作者:行者123 更新时间:2023-11-30 03:01:37 25 4
gpt4 key购买 nike

我正在尝试编写一个程序,它将获取在 EditText 字段中输入的数字并将其转换为整数,以便稍后我可以使用它为应用程序设置计时器。这是我的代码。当我运行它时,它给我一个错误:“Android java.lang.NumberFormatException:Invalid int”。我的错误在哪里?提前致谢

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Start extends Activity {
Button button1;
EditText et;
String timer;
int timer1;
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
button1 = (Button)findViewById(R.id.button1);
et = (EditText)findViewById(R.id.editText1);

bull();


button1.setOnClickListener ( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
int secondsDelayed = 1;
new Handler().postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(Start.this, MainActivity.class);

startActivity(intent);
}
}, secondsDelayed * timer1*1000);
}
});
}
private void bull() {
// TODO Auto-generated method stub
timer1 = Integer.valueOf(et.getText().toString());
}

}

这是我的 LogCat:

03-16 01:39:10.663: I/Process(17577): Sending signal. PID: 17577 SIG: 9
03-16 01:39:10.853: D/AndroidRuntime(18166): Shutting down VM
03-16 01:39:10.853: W/dalvikvm(18166): threadid=1: thread exiting with uncaught exception (group=0x4104fac8)
03-16 01:39:10.863: E/AndroidRuntime(18166): FATAL EXCEPTION: main
03-16 01:39:10.863: E/AndroidRuntime(18166): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.scaryme/com.example.scaryme.Start}: java.lang.NumberFormatException: Invalid int: ""
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.app.ActivityThread.access$700(ActivityThread.java:152)
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.os.Looper.loop(Looper.java:137)
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.app.ActivityThread.main(ActivityThread.java:5328)
03-16 01:39:10.863: E/AndroidRuntime(18166): at java.lang.reflect.Method.invokeNative(Native Method)
03-16 01:39:10.863: E/AndroidRuntime(18166): at java.lang.reflect.Method.invoke(Method.java:511)
03-16 01:39:10.863: E/AndroidRuntime(18166): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
03-16 01:39:10.863: E/AndroidRuntime(18166): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
03-16 01:39:10.863: E/AndroidRuntime(18166): at dalvik.system.NativeStart.main(Native Method)
03-16 01:39:10.863: E/AndroidRuntime(18166): Caused by: java.lang.NumberFormatException: Invalid int: ""
03-16 01:39:10.863: E/AndroidRuntime(18166): at java.lang.Integer.invalidInt(Integer.java:138)
03-16 01:39:10.863: E/AndroidRuntime(18166): at java.lang.Integer.parseInt(Integer.java:359)
03-16 01:39:10.863: E/AndroidRuntime(18166): at java.lang.Integer.parseInt(Integer.java:332)
03-16 01:39:10.863: E/AndroidRuntime(18166): at com.example.scaryme.Start.bull(Start.java:48)
03-16 01:39:10.863: E/AndroidRuntime(18166): at com.example.scaryme.Start.onCreate(Start.java:24)
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.app.Activity.performCreate(Activity.java:5250)
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
03-16 01:39:10.863: E/AndroidRuntime(18166): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-16 01:39:10.863: E/AndroidRuntime(18166): ... 11 more

最佳答案

在你的 onCreate() 方法中你有:

public void onCreate (Bundle savedInstanceState) {
...
et = (EditText)findViewById(R.id.editText1);
bull()
...
}

EditText 的初始内容是一个空字符串。但是,您立即调用 bull(),它会:

timer1 = Integer.valueOf(et.getText().toString());

因为 et 包含一个空字符串(记住,您已经从 onCreate() 调用了它并且编辑框是空的),Integer.valueOf( ) 无法解析它,因为空字符串不是有效整数。 Integer.valueOf() 将抛出您所看到的异常。

您有几个直接的选择:

  • 不要从 onCreate() 调用 bull(),或者
  • 如果编辑文本为空,则使 bull() 不尝试解析字符串。

我推荐第一个,因为您似乎不需要这样做(至少在您的示例中)。

无论如何,在正常操作期间,用户总是有可能不小心输入无效数字,因此您需要通过捕获异常并向用户显示有用的错误消息(例如, “请输入一个有效的整数!”)。

一般来说,当你遇到这样的异常时,你需要查看堆栈跟踪。 onCreate() -> bull() -> 异常代码路径清楚地显示在您的堆栈跟踪中,它还可以准确识别异常和调用发生在哪一行。一旦确定了异常的来源,通常有助于查阅引发异常的方法的文档,这些文档通常描述了引发异常的原因(然后,知道了原因,您就可以解决问题)。例如,Integer.parseInt() documentation .堆栈跟踪是查找意外异常原因的最重要信息之一。

关于java - 字符串到 int。无效整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22430490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com