我不知道我做错了什么,但我有一个名为input的edittext,用户在那里写一个数字,然后你按一个按钮:
input = (EditText) findViewById(R.id.input);
XX= (Button) findViewById(R.id.XX);
XX.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(XX.equals("XX"))
{
String aa= String.valueOf(input) ;
float operation= ((float) ( 0.029 * 2.2 *aa ) );
String cadena= String.valueOf(operation) ;
// set text to cadena (result of the operation)
predic = (TextView) findViewById(R.id.predic);
predic.setText(cadena);
}
问题是我无法执行此操作
float operation =((float)(0.029 * 2.2 * aa));
因为它强调“aa”并说
“运算符'*'不能应用于'double','java.lang.String'”
我认为他认为aa是一个字符串,我怎么能把它作为一个Int来读?
LOGCAT1
09-07 23:18:07.915 10148-10148/com.example.XXXE/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NumberFormatException: unable to parse 'android.widget.EditText@405a8b98' as integer
at java.lang.Integer.parse(Integer.java:383)
at java.lang.Integer.parseInt(Integer.java:372)
at java.lang.Integer.parseInt(Integer.java:332)
***at com.example.XXX.XXX$5.onClick(XXX.java:542)*** CRASHES
at android.view.View.performClick(View.java:2506)
at android.view.View$PerformClick.run(View.java:9112)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)
09-07 23:18:07.965 3698-17447/system_process W/ActivityManager﹕
Force finishing activity com.example.XXXX/.XXXX
答案 0 :(得分:1)
您需要更改:
String aa= String.valueOf(input) ;
到
Int aa = Integer.parseInt(input.getText().toString());
只要您确定用户只会输入整数