Android 工作室 - 致命例外
Android studio - FATAL EXCEPTION
我试图在 android 工作室中的 2 个活动之间切换,问题是每当我按下按钮时程序崩溃并停止工作,这是错误:
> FATAL EXCEPTION: main
Process: com.example.talal.furnitureorder, PID: 19310
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.talal.furnitureorder/com.example.talal.furnitureorder.Invoice}: java.lang.NumberFormatException: Invalid int: "Number of Chairs:"
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NumberFormatException: Invalid int: "Number of Chairs:"
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.talal.furnitureorder.Invoice.onCreate(Invoice.java:48)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我的 XML 代码 activity 1:
<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.example.talal.furnitureorder.Order">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Customer Name"
android:id="@+id/textView"
android:textAlignment="center" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/e1"
android:textAlignment="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Customer Address"
android:id="@+id/textView2"
android:textAlignment="center" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/e2"
android:textAlignment="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Customer City"
android:id="@+id/textView3"
android:textAlignment="center" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/e3"
android:textAlignment="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Number of Chairs ordered"
android:id="@+id/textView4"
android:textAlignment="center" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/e4"
android:textAlignment="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Number of Sofas ordered"
android:id="@+id/textView5"
android:textAlignment="center" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/e5"
android:textAlignment="center" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Print the Invoice"
android:id="@+id/b1"
android:textAlignment="center"
android:onClick="print"
android:layout_marginTop="10pt" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Reset"
android:id="@+id/b2"
android:onClick="reset"
android:textAlignment="center" />
</LinearLayout>
</RelativeLayout>
我的 Java 代码 activity 1:
package com.example.talal.furnitureorder;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class Order extends AppCompatActivity {
private EditText ed1,ed2,ed3,ed4,ed5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
}
public void print (View view){
Intent t=new Intent (this,Invoice.class);
ed1=(EditText) findViewById(R.id.e1);
ed2=(EditText) findViewById(R.id.e2);
ed3=(EditText) findViewById(R.id.e3);
ed4=(EditText) findViewById(R.id.e4);
ed5=(EditText) findViewById(R.id.e5);
Bundle b=new Bundle();
b.putString("name",ed1.getText().toString());
b.putString("address",ed2.getText().toString());
b.putString("city",ed3.getText().toString());
b.putString("C_NO",ed4.getText().toString());
b.putString("S_NO",ed5.getText().toString());
t.putExtras(b);
startActivity(t);
}
}
我的 XML 代码 activity2 :
<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.example.talal.furnitureorder.Invoice">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv1"
android:textAlignment="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv2"
android:textAlignment="center"
android:layout_marginTop="18dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv3"
android:textAlignment="center"
android:layout_marginTop="18dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv4"
android:textAlignment="center"
android:layout_marginTop="18dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv5"
android:textAlignment="center"
android:layout_marginTop="18dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv6"
android:textAlignment="center"
android:layout_marginTop="18dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv7"
android:textAlignment="center"
android:layout_marginTop="18dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv8"
android:textAlignment="center"
android:layout_marginTop="18dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New"
android:id="@+id/b3"
android:textAlignment="center"
android:layout_marginTop="19dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Exit"
android:id="@+id/b4"
android:textAlignment="center"
android:layout_marginTop="10dp" />
</LinearLayout>
</RelativeLayout>
我的 java 代码 activity2:
package com.example.talal.furnitureorder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class Invoice extends AppCompatActivity {
private TextView t1, t2, t3, t4, t5, t6, t7, t8;
int c, s;
int p, tp;
int tx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invoice);
t1 = (TextView) findViewById(R.id.tv1);
t2 = (TextView) findViewById(R.id.tv2);
t3 = (TextView) findViewById(R.id.tv3);
t4 = (TextView) findViewById(R.id.tv4);
t5 = (TextView) findViewById(R.id.tv5);
t6 = (TextView) findViewById(R.id.tv6);
t7 = (TextView) findViewById(R.id.tv7);
t8 = (TextView) findViewById(R.id.tv8);
Bundle bb = getIntent().getExtras();
String s1 = bb.getString("name");
String s2 = bb.getString("address");
String s3 = bb.getString("city");
String s4 = bb.getString("C_NO");
String s5 = bb.getString("S_NO");
t1.setText("Customer Name:" + s1);
t2.setText("Customer Address:" + s2);
t3.setText("Customer City:" + s3);
t4.setText("Number of Chairs:" + s4);
t5.setText("Number of Sofas:" + s5);
c = Integer.parseInt(t4.getText().toString());
s = Integer.parseInt(t5.getText().toString());
p = (350 * c) + (925 * s);
t6.setText("Price is: $" + p);
}
}
c = Integer.parseInt(t4.getText().toString());
你的textview的内容不仅仅是一个数字,这样你就不能把它解析成一个整数,所以它会崩溃。
我不确定你为什么要这样做,但问题出在 Activity2
就在这里:
t4.setText("Number of Chairs:" + s4);
t5.setText("Number of Sofas:" + s5);
c = Integer.parseInt(t4.getText().toString());
s = Integer.parseInt(t5.getText().toString());
您正在将 t4
设置为具有值 "Number of Chairs:" 的 String
,然后尝试提取相同的值并将其 parse
设置为 Integer
- 那不会发生。
不确定您要做什么,但您可能需要 2 个字段 - 一个用于保存 String
,一个用于保存 Integer
TextView t4
包含不是整数的文本 Number of Chairs:
。所以它抛出 NumberFormatException
.
如果您尝试解析从 Order
Activity 检索到的椅子数量,请尝试从
更改您的代码
c = Integer.parseInt(t4.getText().toString());
s = Integer.parseInt(t5.getText().toString());
至
c = Integer.parseInt(s4);
s = Integer.parseInt(s5);
并且由于您正在尝试解析从 EditText
检索到的数据,我建议您使用 try catch block
即
try{
c = Integer.parseInt(s4);
s = Integer.parseInt(s5);
}catch(NumberFormatException ex){ // handle your exception
...
}
更新
发送 0 以防 EditText
为空
Order.java
String c_no = ed4.getText().toString();
String s_np = ed5.getText().toString();
b.putString("C_NO",TextUtils.isEmpty(c_no)? 0 : c_no);
b.putString("S_NO",TextUtils.isEmpty(s_np)? 0 : s_np);
您的问题在这里:
c = Integer.parseInt(t4.getText().toString());
您无法解析字符串 "Number of chairs: 4" f.e 并获取整数。请尝试:
c = Integer.parseInt(s4);
前提是您的 EditText 输入仅允许数字。沙发也一样。
编辑:我在输入时看到上面已经为您提供了答案。
我试图在 android 工作室中的 2 个活动之间切换,问题是每当我按下按钮时程序崩溃并停止工作,这是错误:
> FATAL EXCEPTION: main
Process: com.example.talal.furnitureorder, PID: 19310
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.talal.furnitureorder/com.example.talal.furnitureorder.Invoice}: java.lang.NumberFormatException: Invalid int: "Number of Chairs:"
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NumberFormatException: Invalid int: "Number of Chairs:"
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.talal.furnitureorder.Invoice.onCreate(Invoice.java:48)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我的 XML 代码 activity 1:
<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.example.talal.furnitureorder.Order">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Customer Name"
android:id="@+id/textView"
android:textAlignment="center" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/e1"
android:textAlignment="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Customer Address"
android:id="@+id/textView2"
android:textAlignment="center" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/e2"
android:textAlignment="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Customer City"
android:id="@+id/textView3"
android:textAlignment="center" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/e3"
android:textAlignment="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Number of Chairs ordered"
android:id="@+id/textView4"
android:textAlignment="center" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/e4"
android:textAlignment="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Number of Sofas ordered"
android:id="@+id/textView5"
android:textAlignment="center" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/e5"
android:textAlignment="center" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Print the Invoice"
android:id="@+id/b1"
android:textAlignment="center"
android:onClick="print"
android:layout_marginTop="10pt" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Reset"
android:id="@+id/b2"
android:onClick="reset"
android:textAlignment="center" />
</LinearLayout>
</RelativeLayout>
我的 Java 代码 activity 1:
package com.example.talal.furnitureorder;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class Order extends AppCompatActivity {
private EditText ed1,ed2,ed3,ed4,ed5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
}
public void print (View view){
Intent t=new Intent (this,Invoice.class);
ed1=(EditText) findViewById(R.id.e1);
ed2=(EditText) findViewById(R.id.e2);
ed3=(EditText) findViewById(R.id.e3);
ed4=(EditText) findViewById(R.id.e4);
ed5=(EditText) findViewById(R.id.e5);
Bundle b=new Bundle();
b.putString("name",ed1.getText().toString());
b.putString("address",ed2.getText().toString());
b.putString("city",ed3.getText().toString());
b.putString("C_NO",ed4.getText().toString());
b.putString("S_NO",ed5.getText().toString());
t.putExtras(b);
startActivity(t);
}
}
我的 XML 代码 activity2 :
<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.example.talal.furnitureorder.Invoice">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv1"
android:textAlignment="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv2"
android:textAlignment="center"
android:layout_marginTop="18dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv3"
android:textAlignment="center"
android:layout_marginTop="18dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv4"
android:textAlignment="center"
android:layout_marginTop="18dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv5"
android:textAlignment="center"
android:layout_marginTop="18dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv6"
android:textAlignment="center"
android:layout_marginTop="18dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv7"
android:textAlignment="center"
android:layout_marginTop="18dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv8"
android:textAlignment="center"
android:layout_marginTop="18dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New"
android:id="@+id/b3"
android:textAlignment="center"
android:layout_marginTop="19dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Exit"
android:id="@+id/b4"
android:textAlignment="center"
android:layout_marginTop="10dp" />
</LinearLayout>
</RelativeLayout>
我的 java 代码 activity2:
package com.example.talal.furnitureorder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class Invoice extends AppCompatActivity {
private TextView t1, t2, t3, t4, t5, t6, t7, t8;
int c, s;
int p, tp;
int tx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invoice);
t1 = (TextView) findViewById(R.id.tv1);
t2 = (TextView) findViewById(R.id.tv2);
t3 = (TextView) findViewById(R.id.tv3);
t4 = (TextView) findViewById(R.id.tv4);
t5 = (TextView) findViewById(R.id.tv5);
t6 = (TextView) findViewById(R.id.tv6);
t7 = (TextView) findViewById(R.id.tv7);
t8 = (TextView) findViewById(R.id.tv8);
Bundle bb = getIntent().getExtras();
String s1 = bb.getString("name");
String s2 = bb.getString("address");
String s3 = bb.getString("city");
String s4 = bb.getString("C_NO");
String s5 = bb.getString("S_NO");
t1.setText("Customer Name:" + s1);
t2.setText("Customer Address:" + s2);
t3.setText("Customer City:" + s3);
t4.setText("Number of Chairs:" + s4);
t5.setText("Number of Sofas:" + s5);
c = Integer.parseInt(t4.getText().toString());
s = Integer.parseInt(t5.getText().toString());
p = (350 * c) + (925 * s);
t6.setText("Price is: $" + p);
}
}
c = Integer.parseInt(t4.getText().toString());
你的textview的内容不仅仅是一个数字,这样你就不能把它解析成一个整数,所以它会崩溃。
我不确定你为什么要这样做,但问题出在 Activity2
就在这里:
t4.setText("Number of Chairs:" + s4);
t5.setText("Number of Sofas:" + s5);
c = Integer.parseInt(t4.getText().toString());
s = Integer.parseInt(t5.getText().toString());
您正在将 t4
设置为具有值 "Number of Chairs:" 的 String
,然后尝试提取相同的值并将其 parse
设置为 Integer
- 那不会发生。
不确定您要做什么,但您可能需要 2 个字段 - 一个用于保存 String
,一个用于保存 Integer
TextView t4
包含不是整数的文本 Number of Chairs:
。所以它抛出 NumberFormatException
.
如果您尝试解析从 Order
Activity 检索到的椅子数量,请尝试从
c = Integer.parseInt(t4.getText().toString());
s = Integer.parseInt(t5.getText().toString());
至
c = Integer.parseInt(s4);
s = Integer.parseInt(s5);
并且由于您正在尝试解析从 EditText
检索到的数据,我建议您使用 try catch block
即
try{
c = Integer.parseInt(s4);
s = Integer.parseInt(s5);
}catch(NumberFormatException ex){ // handle your exception
...
}
更新
发送 0 以防 EditText
为空
Order.java
String c_no = ed4.getText().toString();
String s_np = ed5.getText().toString();
b.putString("C_NO",TextUtils.isEmpty(c_no)? 0 : c_no);
b.putString("S_NO",TextUtils.isEmpty(s_np)? 0 : s_np);
您的问题在这里:
c = Integer.parseInt(t4.getText().toString());
您无法解析字符串 "Number of chairs: 4" f.e 并获取整数。请尝试:
c = Integer.parseInt(s4);
前提是您的 EditText 输入仅允许数字。沙发也一样。
编辑:我在输入时看到上面已经为您提供了答案。