tiktak
tiktak

Reputation: 11

Android studio - FATAL EXCEPTION

I am trying to switch between 2 activities in android studio, the problem is whenever I press the button the program crash and stop working, here is the error :

>  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)

my XML code for 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>

my Java code for 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);

    }

    }

my XML code for 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>

my java code for 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);


    }



    }

Upvotes: 0

Views: 1065

Answers (4)

Fabin Paul
Fabin Paul

Reputation: 1711

TextView t4 contains the text Number of Chairs: which is not an integer. So it throws NumberFormatException.

If you are trying to parse number of chairs retrieved from Order Activity, try changing your code from

c = Integer.parseInt(t4.getText().toString());
s = Integer.parseInt(t5.getText().toString());

to

c = Integer.parseInt(s4);
s = Integer.parseInt(s5);

And since your are trying to parse data retrieved from EditText, I would suggest you use try catch block i.e

try{
  c = Integer.parseInt(s4);
  s = Integer.parseInt(s5);
}catch(NumberFormatException ex){ // handle your exception
   ...
}

UPDATE

To send 0 in case EditText is empty

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); 

Upvotes: 1

daxgirl
daxgirl

Reputation: 772

Your problem is here:

 c = Integer.parseInt(t4.getText().toString());

You cannot parse string "Number of chairs: 4" f.e and get integer. Try instead:

 c = Integer.parseInt(s4);

That is provided your EditText input only allowed numbers. Same for sofas btw.

Edit: I can see as I was typing the answer was provided above for you.

Upvotes: 1

Jim
Jim

Reputation: 10288

I'm not sure why you are doing this, but the problem is in Activity2 right here:

    t4.setText("Number of Chairs:" + s4);
    t5.setText("Number of Sofas:" + s5);

    c = Integer.parseInt(t4.getText().toString());
    s = Integer.parseInt(t5.getText().toString());

You are setting t4 to a String with the value "Number of Chairs:" and then trying to pull that same value out and parse it to an Integer - that's not gonna happen.

Not sure what you are trying to do, but it's likely you will need 2 Fields - one to hold the String and one to hold the Integer

Upvotes: 2

Stefan
Stefan

Reputation: 2178

    c = Integer.parseInt(t4.getText().toString());

The content of your textview is more than just a number, this way you can't parse it to an integer, therefore it crashes.

Upvotes: 0

Related Questions