【发布时间】:2014-03-15 22:20:28
【问题描述】:
我正在尝试编写一个程序,该程序将获取 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
【问题讨论】:
-
您在以下 oschlueter 的回答中的 cmets 与您的问题描述不符。您在此处的问题中声明,当应用程序运行时您会收到 NumberFormatException。如果不是这种情况,请在您的问题中提供对您的问题的真实、准确的描述。还请包括任何堆栈跟踪或错误日志,并请在源 sn-ps 中明确标记与您看到的异常相对应的行,因为此处粘贴的代码不显示行号。跨度>
-
感谢您更新您的问题。我已经更新了我的答案。