【发布时间】:2017-09-08 07:46:40
【问题描述】:
我不知道这里出了什么问题。 代码片段
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String s1=SpinnerObj.getSelectedItem().toString();
String n1=s1.substring(s1.indexOf("Rs")+2,s1.length()-1);
int ns1=Integer.parseInt(String.valueOf(n1));//Couldnt cast and throws Exception
Toast.makeText(getBaseContext(),""+ns1,Toast.LENGTH_LONG).show();
Intent i = new Intent(getBaseContext(),OrderPlaced.class);
startActivity(i);
}
catch (NumberFormatException nfe) {
Toast.makeText(getBaseContext(), "gg", Toast.LENGTH_LONG).show();
}
}});
我在这里尝试将从 Spinner 获取的字符串类型转换为整数以进行计算。
【问题讨论】:
-
n1 的值是多少?
-
添加错误logcat ...
-
当String超过整数时有时无法将其转换为int。它会抛出异常。尝试使用使用字符串值并检查一次
-
确保您的 n1 字符串中没有空格或任何其他字符串值,它必须只包含数值。 OR 可能是 n1 值为空。也发布 logcat 和您的字符串以获得更清晰的想法。
-
我的微调器值为“Havells 风扇(900 卢比)”。并且 m 在 n1 中得到 900。