我制作了一个计算器的应用程序,用户将在其中提供数字,点击按钮即可获得输出。
当我在模拟器中运行它时,它显示“Unfortunately, <app> has stopped
”。
我的Logcat是:
01-15 05:56:24.200: E/AndroidRuntime(874): FATAL EXCEPTION: main
01-15 05:56:24.200: E/AndroidRuntime(874): Process: com1.example.calculator, PID: 874
01-15 05:56:24.200: E/AndroidRuntime(874): java.lang.IllegalStateException: Could not
execute method of the activity
01-15 05:56:24.200: E/AndroidRuntime(874): at android.view.View$1.onClick(View.java:3814)
01-15 05:56:24.200: E/AndroidRuntime(874): at android.view.View.performClick(View.java:4424)
01-15 05:56:24.200: E/AndroidRuntime(874): at android.view.View$PerformClick.run(View.java:18383)
01-15 05:56:24.200: E/AndroidRuntime(874): at android.os.Handler.handleCallback(Handler.java:733)
01-15 05:56:24.200: E/AndroidRuntime(874): at android.os.Handler.dispatchMessage(Handler.java:95)
01-15 05:56:24.200: E/AndroidRuntime(874): at android.os.Looper.loop(Looper.java:137)
01-15 05:56:24.200: E/AndroidRuntime(874): at android.app.ActivityThread.main(ActivityThread.java:4998)
01-15 05:56:24.200: E/AndroidRuntime(874): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 05:56:24.200: E/AndroidRuntime(874): at java.lang.reflect.Method.invoke(Method.java:515)
01-15 05:56:24.200: E/AndroidRuntime(874): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-15 05:56:24.200: E/AndroidRuntime(874): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-15 05:56:24.200: E/AndroidRuntime(874): at dalvik.system.NativeStart.main(Native Method)
01-15 05:56:24.200: E/AndroidRuntime(874): Caused by: java.lang.reflect.InvocationTargetException
01-15 05:56:24.200: E/AndroidRuntime(874): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 05:56:24.200: E/AndroidRuntime(874): at java.lang.reflect.Method.invoke(Method.java:515)
01-15 05:56:24.200: E/AndroidRuntime(874): at android.view.View$1.onClick(View.java:3809)
01-15 05:56:24.200: E/AndroidRuntime(874): ... 11 more
01-15 05:56:24.200: E/AndroidRuntime(874): Caused by: java.lang.NumberFormatException: Invalid int: ""
01-15 05:56:24.200: E/AndroidRuntime(874): at java.lang.Integer.invalidInt(Integer.java:137)
01-15 05:56:24.200: E/AndroidRuntime(874): at java.lang.Integer.parseInt(Integer.java:358)
01-15 05:56:24.200: E/AndroidRuntime(874): at java.lang.Integer.parseInt(Integer.java:331)
01-15 05:56:24.200: E/AndroidRuntime(874): at com1.example.calculator.Cal.Add(Cal.java:26)
01-15 05:56:24.200: E/AndroidRuntime(874): ... 14 more
我的代码:
package com1.example.calculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class Cal extends Activity {
EditText e1,e2,e3;
int a,b,c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cal);
}
public void Add(View v)
{
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText2);
e1=(EditText)findViewById(R.id.editText3);
String s1=e1.getText().toString();
String s2=e2.getText().toString();
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
c=a+b;
String s3=Integer.toString(c);
e3.setText(s3);
}
public void sub(View v)
{
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText2);
e1=(EditText)findViewById(R.id.editText3);
String s1=e1.getText().toString();
String s2=e2.getText().toString();
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
c=a-b;
String s3=Integer.toString(c);
e3.setText(s3);
}
public void Mul (View v)
{
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText2);
e1=(EditText)findViewById(R.id.editText3);
String s1=e1.getText().toString();
String s2=e2.getText().toString();
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
c=a*b;
String s3=Integer.toString(c);
e3.setText(s3);
}
public void Div (View v)
{
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText2);
e1=(EditText)findViewById(R.id.editText3);
String s1=e1.getText().toString();
String s2=e2.getText().toString();
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
c=a/b;
String s3=Integer.toString(c);
e3.setText(s3);
}
public void Percentage(View v)
{
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText2);
e1=(EditText)findViewById(R.id.editText3);
String s1=e1.getText().toString();
String s2=e2.getText().toString();
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
c=(a*b)/100;
String s3=Integer.toString(c);
e3.setText(s3);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.cal, menu);
return true;
}``
}
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=".Cal" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="21dp"
android:layout_marginTop="27dp"
android:ems="10"
android:inputType="number"/>
<requestFocus/>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="number" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button3"
android:layout_toRightOf="@+id/button3"
android:onClick="Sub"
android:text="Subtraction" />
答案 0 :(得分:0)
当您尝试解析为空的String时,会发生此错误onClick
方法。你能用代码来识别确切的行。
只需验证您的文字是否为空EditText.getText().isEmpty()
,即使没有此验证,您也可以获得零除法异常。