我编写此代码是为了简化数学中的部件号,例如2a / 2b = a / b。 android studio显示没有问题,但是当我运行此代码时:(maz代表最低数字,liels代表最高数字,skait是上一个示例中的2a,sauc是上一个示例中的2b int,skaitout和saucout是输出文本对应于零件号部分的字段,skaitout是最高编号,而saucout是底部编号)
package com.example.mikus.simplify;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View skait = findViewById(R.id.skait);
View sauc = findViewById(R.id.sauc);
TextView skaitout = (TextView) findViewById(R.id.skaitout);
TextView saucout = (TextView) findViewById(R.id.saucout);
int maz;
int liels;
int x;
int i = 0;
maz = Integer.parseInt(skait.toString());
liels = Integer.parseInt(sauc.toString());
if(maz > liels){
x = maz;
maz = liels;
liels = x;
i = 1;
}
if(maz == liels){
skaitout.setText('1');
saucout.setText('1');
}else{
x = maz;
while(true){
if(maz % x == 0 && liels % x == 0){
maz /= x;
liels /= x;
x = maz;
}else{
if(x == liels){
return;
}else{
x++;
}
}
}
}
if (i == 0) {
skaitout.setText(maz);
saucout.setText(liels);
}
if(i == 1){
skaitout.setText(liels);
saucout.setText(maz);
}
}
}
我收到错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mikus.simplify, PID: 24905
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mikus.simplify/com.example.mikus.simplify.MainActivity}: java.lang.NumberFormatException: Invalid int: "android.support.v7.widget.AppCompatEditText{11f3506 VFED..CL. ......ID 0,0-0,0 #7f0b0057 app:id/skait}"
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352)
at android.app.ActivityThread.access$1100(ActivityThread.java:223)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NumberFormatException: Invalid int: "android.support.v7.widget.AppCompatEditText{11f3506 VFED..CL. ......ID 0,0-0,0 #7f0b0057 app:id/skait}"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:410)
at java.lang.Integer.parseInt(Integer.java:367)
at java.lang.Integer.parseInt(Integer.java:334)
at com.example.mikus.simplify.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:6877)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352)
at android.app.ActivityThread.access$1100(ActivityThread.java:223)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
在手机中,程序崩溃了。任何人都可以帮助我。
答案 0 :(得分:3)
您尝试将视图转换为字符串并将其解析为整数:
liels = Integer.parseInt(sauc.toString());
maz = Integer.parseInt(skait.toString());
我想你想做这样的事情:
liels = Integer.parseInt(sauc.getText().toString());
maz = Integer.parseInt(skait.getText().toString());
此外,用户可能会输入错误的内容。如果输入不是如下数字,您可以捕获异常:
try{
liels = Integer.parseInt(sauc.getText().toString());
maz = Integer.parseInt(skait.getText().toString());
}catch(NumberFormatException ex){
//inform the user about wrong input
}
或者您可以通过将此属性设置为editTexts来限制用户输入:
android:inputType="number"
请注意:稍后您将通过设置文本获得例外:
if (i == 0) {
skaitout.setText(maz);
saucout.setText(liels);
}
if(i == 1){
skaitout.setText(liels);
saucout.setText(maz);
}
这将引发异常,您必须将整数转换为如下字符串:
if (i == 0) {
skaitout.setText(Integer.toString(maz));
saucout.setText(Integer.toString(liels));
}
if(i == 1){
skaitout.setText(Integer.toString(liels));
saucout.setText(Integer.toString(maz));
}
或仅举例说明一些引号:
saucout.setText(""+maz);
答案 1 :(得分:0)
您尝试将View
解析为int
。