我在android上创建了两个Integer变量的小例子,并通过Toast小部件在屏幕上显示结果但总是得到消息“不幸的是First has stopped”,我在log cat中看到一些例外,当我删除时app正常运行代码中的三个int变量
logCat `
05-06 14:51:35.140: D/AndroidRuntime(1201): Shutting down VM
05-06 14:51:35.140: W/dalvikvm(1201): threadid=1: thread exiting with uncaught exception (group=0xa623a288)
05-06 14:51:35.140: E/AndroidRuntime(1201): FATAL EXCEPTION: main
05-06 14:51:35.140: E/AndroidRuntime(1201): java.lang.RuntimeException: Unable to start activity ComponentInfo{Expert.MML/Expert.MML.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
05-06 14:51:35.140: E/AndroidRuntime(1201): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-06 14:51:35.140: E/AndroidRuntime(1201): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-06 14:51:35.140: E/AndroidRuntime(1201): at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-06 14:51:35.140: E/AndroidRuntime(1201): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-06 14:51:35.140: E/AndroidRuntime(1201): at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 14:51:35.140: E/AndroidRuntime(1201): at android.os.Looper.loop(Looper.java:137)
05-06 14:51:35.140: E/AndroidRuntime(1201): at android.app.ActivityThread.main(ActivityThread.java:4745)
05-06 14:51:35.140: E/AndroidRuntime(1201): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 14:51:35.140: E/AndroidRuntime(1201): at java.lang.reflect.Method.invoke(Method.java:511)
05-06 14:51:35.140: E/AndroidRuntime(1201): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-06 14:51:35.140: E/AndroidRuntime(1201): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-06 14:51:35.140: E/AndroidRuntime(1201): at dalvik.system.NativeStart.main(Native Method)
05-06 14:51:35.140: E/AndroidRuntime(1201): Caused by: java.lang.NumberFormatException: Invalid int: ""
05-06 14:51:35.140: E/AndroidRuntime(1201): at java.lang.Integer.invalidInt(Integer.java:138)
05-06 14:51:35.140: E/AndroidRuntime(1201): at java.lang.Integer.parseInt(Integer.java:359)
05-06 14:51:35.140: E/AndroidRuntime(1201): at java.lang.Integer.parseInt(Integer.java:332)
05-06 14:51:35.140: E/AndroidRuntime(1201): at Expert.MML.MainActivity$1.<init>(MainActivity.java:25)
05-06 14:51:35.140: E/AndroidRuntime(1201): at Expert.MML.MainActivity.onCreate(MainActivity.java:24)
05-06 14:51:35.140: E/AndroidRuntime(1201): at android.app.Activity.performCreate(Activity.java:5008)
05-06 14:51:35.140: E/AndroidRuntime(1201): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-06 14:51:35.140: E/AndroidRuntime(1201): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-06 14:51:35.140: E/AndroidRuntime(1201): ... 11 more
此MainActivity类
package Expert.MML;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById(R.id.button1);
final EditText v1 = (EditText) findViewById(R.id.value1);
final EditText v2 = (EditText) findViewById(R.id.value2);
btn1.setText("push");
btn1.setOnClickListener(new View.OnClickListener() {
int val1 = Integer.parseInt(v1.getText().toString());
int val2 = Integer.parseInt(v2.getText().toString());
int result = val1 + val2;
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "The total value is : " + result, Toast.LENGTH_LONG).show();
}
});
}
}
和此activity_main.xml
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/AbsoluteLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<EditText
android:id="@+id/value1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="58dp"
android:layout_y="26dp"
android:ems="10"
android:inputType="number" />
<EditText
android:id="@+id/value2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="59dp"
android:layout_y="77dp"
android:ems="10"
android:inputType="number" />
<Button
android:id="@+id/button1"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:layout_x="118dp"
android:layout_y="135dp"
android:text="Button" />
</AbsoluteLayout>
答案 0 :(得分:2)
如果您的文本值为空,则无法解析其中的整数。 所以你需要检查它们是否为空。
try{
int val1 = Integer.parseInt(v1.getText().toString());
int val2 = Integer.parseInt(v2.getText().toString());
}
catch(NumberFormatException e){
Toast.makeText(MainActivity.this, "Empty or invalid input",
Toast.LENGTH_LONG).show();
return;
}