java当我单击按钮时,我的应用程序意外停止
如果我将某些文本框留空,然后单击按钮“应用程序意外停止”,我需要通过将未填充的文本框指定为0来计算总和,然后计算总和。请提供一些解决方案
package com.sabari.myapplication;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.text.InputType;
import 安卓.text.TextUtils;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.TextView;
import 安卓.text.Editable;
public class MainActivity extends AppCompatActivity {
TextView tv1;
TextView tv2;
TextView tv3;
TextView tv4;
TextView tv5;
TextView tv6;
TextView tv7;
TextView tv8;
TextView tv10;
EditText et1;
EditText et2;
EditText et3;
EditText et4;
EditText et5;
EditText et6;
EditText et7;
EditText et8;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView)findViewById(R.id.textView);
tv2 = (TextView)findViewById(R.id.textView2);
tv3 = (TextView)findViewById(R.id.textView3);
tv4 = (TextView)findViewById(R.id.textView4);
tv5 = (TextView)findViewById(R.id.textView5);
tv6 = (TextView)findViewById(R.id.textView6);
tv7 = (TextView)findViewById(R.id.textView7);
tv8 = (TextView)findViewById(R.id.textView8);
b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et1 = (EditText)findViewById(R.id.editText);
et2 = (EditText)findViewById(R.id.editText2);
et3 = (EditText)findViewById(R.id.editText3);
et4 = (EditText)findViewById(R.id.editText4);
et5 = (EditText)findViewById(R.id.editText5);
et6 = (EditText)findViewById(R.id.editText6);
et7 = (EditText)findViewById(R.id.editText7);
et8 = (EditText)findViewById(R.id.editText8);
tv10 = (TextView)findViewById(R.id.textView10);
int n;
double no1,no2,no3,no4,no5,no6,no7,no8;
no1 = Double.parseDouble(et1.getText().toString());
no2 = Double.parseDouble(et2.getText().toString());
no3 = Double.parseDouble(et3.getText().toString());
no4 = Double.parseDouble(et4.getText().toString());
no5 = Double.parseDouble(et5.getText().toString());
no6 = Double.parseDouble(et6.getText().toString());
no7 = Double.parseDouble(et7.getText().toString());
no8 = Double.parseDouble(et8.getText().toString());
if (no2 <= 0||TextUtils.isEmpty(et2.getText().toString())) {
no2=0;
n = 1;
} else if (no3 <= 0||TextUtils.isEmpty(et3.getText())) {
no3=0;
n = 2;
} else if (no4 <= 0||TextUtils.isEmpty(et4.getText())) {
no4=0;
n = 3;
} else if (no5 <= 0||TextUtils.isEmpty(et5.getText())) {
no5=0;
n = 4;
} else if (no6 <= 0||TextUtils.isEmpty(et6.getText())) {
no6=0;
n = 5;
} else if (no7 <= 0||TextUtils.isEmpty(et7.getText())) {
no7=0;
n = 6;
} else if (no8 <= 0||TextUtils.isEmpty(et8.getText())) {
no8=0;
n = 7;
} else {
n = 8;
}
double res=0;
res = (no1+no2+no3+no4+no5+no6+no7+no8)/n;
tv10.setText(""+res);
}
});
}
}
这是我的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sabari.myapplication.MainActivity">
<ScrollView
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent">
<LinearLayout
安卓:orientation="vertical"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:text="Enter Your 1st Semester GPA"
安卓:id="@+id/textView" />
<EditText
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:inputType="numberDecimal"
安卓:ems="10"
安卓:id="@+id/editText" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:text="Enter Your 2nd Semester GPA"
安卓:id="@+id/textView2" />
<EditText
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:inputType="numberDecimal"
安卓:ems="10"
安卓:id="@+id/editText2" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:text="Enter Your 3rd Semester GPA"
安卓:id="@+id/textView3" />
<EditText
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:inputType="numberDecimal"
安卓:ems="10"
安卓:id="@+id/editText3"
安卓:layout_gravity="center_horizontal" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:text="Enter Your 4th Semester GPA"
安卓:id="@+id/textView4" />
<EditText
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:inputType="numberDecimal"
安卓:ems="10"
安卓:id="@+id/editText4" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:text="Enter Your 5th Semester GPA"
安卓:id="@+id/textView5" />
<EditText
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:inputType="numberDecimal"
安卓:ems="10"
安卓:id="@+id/editText5" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:text="Enter Your 6th Semester GPA"
安卓:id="@+id/textView6" />
<EditText
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:inputType="numberDecimal"
安卓:ems="10"
安卓:id="@+id/editText6" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:text="Enter Your 7th Semester GPA"
安卓:id="@+id/textView7" />
<EditText
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:inputType="numberDecimal"
安卓:ems="10"
安卓:id="@+id/editText7"
安卓:layout_gravity="center_horizontal" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceSmall"
安卓:text="Enter Your 8th Semester GPA"
安卓:id="@+id/textView8" />
<EditText
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:inputType="numberDecimal"
安卓:ems="10"
安卓:id="@+id/editText8"
安卓:layout_gravity="center_horizontal" />
<Button
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="New Button"
安卓:id="@+id/button"
安卓:layout_gravity="center_horizontal" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="New Text"
安卓:id="@+id/textView10" />
</LinearLayout>
</ScrollView>
</LinearLayout>
这是我的日志
08-01 23:11:06.724 15049-15049/com.sabari.myapplication I/art: Not late-enabling -Xcheck:jni (already on)
08-01 23:11:07.906 15049-15049/com.sabari.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.sabari.myapplication-1/lib/x86
08-01 23:11:15.089 15049-15049/com.sabari.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.sabari.myapplication-1/lib/x86
08-01 23:11:16.037 15049-15049/com.sabari.myapplication W/art: Before Android 4.1, method 安卓.graphics.PorterDuffColorFilter 安卓.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(安卓.graphics.PorterDuffColorFilter, 安卓.content.res.ColorStateList, 安卓.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in 安卓.graphics.drawable.Drawable
08-01 23:11:16.079 15049-15055/com.sabari.myapplication W/art: Suspending all threads took: 6.289ms
08-01 23:11:16.254 15049-15100/com.sabari.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
[ 08-01 23:11:16.333 15049:15049 D/ ]
HostConnection::get() New Host Connection established 0xaa9a85f0, tid 15049
08-01 23:11:16.825 15049-15100/com.sabari.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
08-01 23:11:17.577 15049-15100/com.sabari.myapplication W/EGL_emulation: eglSurfaceAttrib not implemented
08-01 23:11:17.577 15049-15100/com.sabari.myapplication W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xae63fc40, error=EGL_SUCCESS
08-01 23:11:17.874 15049-15049/com.sabari.myapplication I/Choreographer: Skipped 66 frames! The application may be doing too much work on its main thread.
08-01 23:11:19.488 15049-15049/com.sabari.myapplication I/Choreographer: Skipped 70 frames! The application may be doing too much work on its main thread.
08-01 23:11:41.841 15049-15049/com.sabari.myapplication D/AndroidRuntime: Shutting down VM
08-01 23:11:41.848 15049-15049/com.sabari.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sabari.myapplication, PID: 15049
java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at com.sabari.myapplication.MainActivity$1.onClick(MainActivity.java:83)
at 安卓.view.View.performClick(View.java:5198)
at 安卓.view.View$PerformClick.run(View.java:21147)
at 安卓.os.Handler.handleCallback(Handler.java:739)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:148)
at 安卓.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-01 23:11:47.178 15049-15049/com.sabari.myapplication I/Process: Sending signal. PID: 15049 SIG: 9
08-01 23:31:19.040 15155-15155/com.sabari.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.sabari.myapplication-1/lib/x86
08-01 23:31:21.349 15155-15155/com.sabari.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.sabari.myapplication-1/lib/x86
08-01 23:31:21.417 15155-15155/com.sabari.myapplication W/art: Before Android 4.1, method 安卓.graphics.PorterDuffColorFilter 安卓.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(安卓.graphics.PorterDuffColorFilter, 安卓.content.res.ColorStateList, 安卓.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in 安卓.graphics.drawable.Drawable
08-01 23:31:21.471 15155-15209/com.sabari.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
[ 08-01 23:31:21.474 15155:15155 D/ ]
HostConnection::get() New Host Connection established 0xad17f430, tid 15155
[ 08-01 23:31:21.516 15155:15209 D/ ]
HostConnection::get() New Host Connection established 0xad17f6d0, tid 15209
08-01 23:31:21.522 15155-15209/com.sabari.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
08-01 23:31:21.599 15155-15209/com.sabari.myapplication W/EGL_emulation: eglSurfaceAttrib not implemented
08-01 23:31:21.599 15155-15209/com.sabari.myapplication W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaaabbd00, error=EGL_SUCCESS
08-01 23:31:37.374 15155-15155/com.sabari.myapplication D/AndroidRuntime: Shutting down VM
08-01 23:31:37.375 15155-15155/com.sabari.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sabari.myapplication, PID: 15155
java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at com.sabari.myapplication.MainActivity$1.onClick(MainActivity.java:83)
at 安卓.view.View.performClick(View.java:5198)
at 安卓.view.View$PerformClick.run(View.java:21147)
at 安卓.os.Handler.handleCallback(Handler.java:739)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:148)
at 安卓.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-01 23:31:48.516 15155-15155/com.sabari.myapplication I/Process: Sending signal. PID: 15155 SIG: 9
# 1 楼答案
如果
str = et1.getText().toString()
的结果为空,则调用Double.parseDouble(str)
将抛出NumberFormatException。在尝试解析字符串之前,您需要检查字符串是否为空,或者将每个解析调用包装在一个try-catch块中(无论如何,这是一个好主意,因为如果文本包含无法解析为double的内容,会发生什么情况另外,et1的回报类型是什么。getText()?如果是字符串,则无需对其调用toString()
# 2 楼答案
当你有空的EditExt时,就像你在问题中说的,这些行:
将抛出一个未破坏的
java.lang.NumberFormatException
。你需要把它们放在一个试捕区。此外,重构代码。如果在Android Studio中,按Ctrl+Alt+L重新排列代码