我已经在Eclipse中编写了android计算器程序,并且在我完成代码时没有出现错误,但是当模拟器运行时,例如在方程式运算符单击时输入两个数字求和,此错误出现& #34;遗憾的是应用程序停止了#34;
我的Main.java
package com.Tools.s01_e07;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class Main extends Activity {
private TextView show;
private ImageView n1;
private ImageView n2;
private ImageView n3;
private ImageView n4;
private ImageView n5;
private ImageView n6;
private ImageView n7;
private ImageView n8;
private ImageView n9;
private ImageView n0;
private ImageView nf1;
private ImageView nf2;
private ImageView nf3;
private ImageView nf4;
private ImageView nf5;
private ImageView nf6;
private int V1;
private String F;
private int V2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
n1=(ImageView) findViewById(R.id.n1);
n2=(ImageView) findViewById(R.id.n2);
n3=(ImageView) findViewById(R.id.n3);
n4=(ImageView) findViewById(R.id.n4);
n5=(ImageView) findViewById(R.id.n5);
n6=(ImageView) findViewById(R.id.n6);
n7=(ImageView) findViewById(R.id.n7);
n8=(ImageView) findViewById(R.id.n8);
n9=(ImageView) findViewById(R.id.n9);
n0=(ImageView) findViewById(R.id.n0);
nf1=(ImageView) findViewById(R.id.nf1);
nf2=(ImageView) findViewById(R.id.nf2);
nf3=(ImageView) findViewById(R.id.nf3);
nf4=(ImageView) findViewById(R.id.nf4);
nf5=(ImageView) findViewById(R.id.nf5);
nf6=(ImageView) findViewById(R.id.nf6);
show=(TextView) findViewById(R.id.show);
n1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
show.setText(show.getText()+"1");
}
});
n2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
show.setText(show.getText()+"2");
}
});
n3.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
show.setText(show.getText()+"3");
}
});
n4.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
show.setText(show.getText()+"4");
}
});
n5.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
show.setText(show.getText()+"5");
}
});
n6.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
show.setText(show.getText()+"6");
}
});
n7.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
show.setText(show.getText()+"7");
}
});
n8.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
show.setText(show.getText()+"8");
}
});
n9.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
show.setText(show.getText()+"9");
}
});
n0.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
show.setText(show.getText()+"0");
}
});
nf1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
V1=Integer.parseInt(show.getText().toString());
F="+";
show.setText(" ");
}
});
nf2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
V1=Integer.parseInt(show.getText().toString());
F="-";
show.setText(" ");
}
});
nf3.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
V1=Integer.parseInt(show.getText().toString());
F="*";
show.setText(" ");
}
});
nf4.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
V1=Integer.parseInt(show.getText().toString());
F="/";
show.setText(" ");
}
});
nf5.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(F!="" && show.getText().toString()!="")
{
int res=0;
V2=Integer.parseInt(show.getText().toString());
if(F=="+"){
res=V1+V2;
}
if(F=="-"){
res=V1-V2;
}
if(F=="*"){
res=V1*V2;
}
if(F=="/"){
res=V1/V2;
}
show.setText(res+"");
}
}
});
nf6.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
}
});
}
}

main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.Tools.s01_e07.Main" >
<ImageView
android:id="@+id/n1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="116dp"
android:layout_marginLeft="64dp"
android:src="@drawable/a01" />
<ImageView
android:id="@+id/n2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/n1"
android:layout_toRightOf="@+id/n1"
android:src="@drawable/a02" />
<ImageView
android:id="@+id/n3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/n2"
android:layout_toRightOf="@+id/n2"
android:src="@drawable/a03" />
<ImageView
android:id="@+id/n4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/n1"
android:layout_alignLeft="@+id/n1"
android:src="@drawable/a04" />
<ImageView
android:id="@+id/n5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/n1"
android:layout_toRightOf="@+id/n1"
android:src="@drawable/a05" />
<ImageView
android:id="@+id/n6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/n3"
android:layout_alignTop="@+id/n5"
android:src="@drawable/a06" />
<ImageView
android:id="@+id/n7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/n4"
android:layout_alignLeft="@+id/n4"
android:src="@drawable/a07" />
<ImageView
android:id="@+id/n8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/n7"
android:layout_toRightOf="@+id/n7"
android:src="@drawable/a08" />
<ImageView
android:id="@+id/n9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/n6"
android:layout_alignTop="@+id/n8"
android:src="@drawable/a09" />
<ImageView
android:id="@+id/n0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/n2"
android:layout_toRightOf="@+id/n1"
android:src="@drawable/a00" />
<ImageView
android:id="@+id/nf5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/n2"
android:layout_toRightOf="@+id/n2"
android:src="@drawable/mosavi" />
<ImageView
android:id="@+id/nf1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/n9"
android:layout_toRightOf="@+id/n9"
android:src="@drawable/sum" />
<ImageView
android:id="@+id/nf2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/nf1"
android:layout_below="@+id/nf1"
android:src="@drawable/tafrigh" />
<ImageView
android:id="@+id/nf3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nf2"
android:layout_toRightOf="@+id/n3"
android:src="@drawable/zarb" />
<ImageView
android:id="@+id/nf4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/nf3"
android:layout_below="@+id/nf3"
android:src="@drawable/taghsim" />
<ImageView
android:id="@+id/nf6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/n1"
android:layout_alignTop="@+id/nf4"
android:src="@drawable/p2" />
<TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/n7"
android:layout_alignLeft="@+id/n7"
android:layout_alignRight="@+id/nf1"
android:layout_marginBottom="40dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="22sp" />
</RelativeLayout>
&#13;
这是我的LogCat
03-20 14:59:37.211: D/dalvikvm(1225): GC_CONCURRENT freed 69K, 5% free 6286K/6616K, paused 3ms+2ms, total 26ms
03-20 14:59:37.283: D/libEGL(1225): loaded /system/lib/egl/libEGL_genymotion.so
03-20 14:59:37.287: D/(1225): HostConnection::get() New Host Connection established 0xb78f8410, tid 1225
03-20 14:59:37.299: D/libEGL(1225): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
03-20 14:59:37.299: D/libEGL(1225): loaded /system/lib/egl/libGLESv2_genymotion.so
03-20 14:59:37.355: W/EGL_genymotion(1225): eglSurfaceAttrib not implemented
03-20 14:59:37.367: D/OpenGLRenderer(1225): Enabling debug mode 0
03-20 15:00:01.239: D/AndroidRuntime(1225): Shutting down VM
03-20 15:00:01.239: W/dalvikvm(1225): threadid=1: thread exiting with uncaught exception (group=0xa613f908)
03-20 15:00:01.239: E/AndroidRuntime(1225): FATAL EXCEPTION: main
03-20 15:00:01.239: E/AndroidRuntime(1225): java.lang.NumberFormatException: Invalid int: " 2"
03-20 15:00:01.239: E/AndroidRuntime(1225): at java.lang.Integer.invalidInt(Integer.java:138)
03-20 15:00:01.239: E/AndroidRuntime(1225): at java.lang.Integer.parse(Integer.java:375)
03-20 15:00:01.239: E/AndroidRuntime(1225): at java.lang.Integer.parseInt(Integer.java:366)
03-20 15:00:01.239: E/AndroidRuntime(1225): at java.lang.Integer.parseInt(Integer.java:332)
03-20 15:00:01.239: E/AndroidRuntime(1225): at com.Tools.s01_e07.Main$15.onClick(Main.java:173)
03-20 15:00:01.239: E/AndroidRuntime(1225): at android.view.View.performClick(View.java:4204)
03-20 15:00:01.239: E/AndroidRuntime(1225): at android.view.View$PerformClick.run(View.java:17355)
03-20 15:00:01.239: E/AndroidRuntime(1225): at android.os.Handler.handleCallback(Handler.java:725)
03-20 15:00:01.239: E/AndroidRuntime(1225): at android.os.Handler.dispatchMessage(Handler.java:92)
03-20 15:00:01.239: E/AndroidRuntime(1225): at android.os.Looper.loop(Looper.java:137)
03-20 15:00:01.239: E/AndroidRuntime(1225): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-20 15:00:01.239: E/AndroidRuntime(1225): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 15:00:01.239: E/AndroidRuntime(1225): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 15:00:01.239: E/AndroidRuntime(1225): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-20 15:00:01.239: E/AndroidRuntime(1225): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-20 15:00:01.239: E/AndroidRuntime(1225): at dalvik.system.NativeStart.main(Native Method)
&#13;
答案 0 :(得分:1)
按下操作员按钮时,将show TextView设置为“”(空格),然后将第二个操作符的数字附加到其上。 你可能在parseInt中得到一个NumberFormatException。 您也应该不相信用户正常按下按钮。