我创建了一个计时器,该计时器将通过edittext框接受用户指定的时间值,并将其传递给CountDownTimer().这个方法期望一个长值,这就是为什么我将其转换为long的原因,但是当我添加这个长转换时,不幸的是android模拟器显示了一个错误.这是我的代码
EditText ti=(EditText)findViewById(R.id.time);
Editable i = ti.getText();
String p=i.toString();
long x=Long.parseLong(p);
final TextView mCounter1TextField=(TextView)findViewById(R.id.counter1);
final CountDownTimer Counter1 = new CountDownTimer(x, 10) {
public void onTick(long millisUntilFinished) {
mCounter1TextField.setText(" " + (millisUntilFinished)/ 1000 + ":");
}
public void onFinish() {
mp3.start ();
Log.d ("Splash", "LauncherActivity.onCreate - created MediaPlayer");
cancel();
}
};
//Start Button1
btnstart.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Counter1.start();
}
};
解决方法:
没有错误可以继续,我注意到的第一件事是您没有在parseLong()上捕获NumberFormatException
long x=0L;
try {
x = Long.parseLong(p);
}
catch(NumberFormatException ex) {
// TODO: error report or something
}
标签:long-integer,casting,android-emulator,string,android
来源: https://codeday.me/bug/20191127/2076515.html