0

App crashing after button is pressed.

below is the stacktrace:

FATAL EXCEPTION: FATAL EXCEPTION: main
Process: com.example.btyagi.tipcalculator, PID: 8357
android.content.res.Resources$NotFoundException: String resource ID #0x0
    at android.content.res.Resources.getText(Resources.java:312)
    at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
    at android.widget.TextView.setText(TextView.java:4417)
    at com.example.btyagi.tipcalculator.MainActivity$1.onClick(MainActivity.java:54)
    at android.view.View.performClick(View.java:5198)
    at android.view.View$PerformClick.run(View.java:21147)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    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)

This is my Activity class:

public class MainActivity extends AppCompatActivity {
    EditText totalBill;
    EditText totalTip;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final EditText checkAmount = (EditText) findViewById(R.id.editText);
        final EditText noOfPeople = (EditText) findViewById(R.id.editText2);
        final EditText tipPercent = (EditText) findViewById(R.id.editText3);

        totalBill = (EditText) findViewById(R.id.editText4);
        totalTip = (EditText) findViewById(R.id.editText5);


        //TextView totalPerPerson = (TextView) findViewById(R.id.textView5);
        //TextView tipPerPerson = (TextView) findViewById(R.id.textView4);


        Button calculateButton = (Button) findViewById(R.id.button);
//        ArrayList<String> list = new ArrayList<String>();
//        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,list);


        // try {


        calculateButton.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                try {
                     int amount = Integer.parseInt(checkAmount.getText().toString());
                     int people = Integer.parseInt(noOfPeople.getText().toString());
                     int tip = Integer.parseInt(tipPercent.getText().toString());
                    totalBill.setText(new StringBuilder().append(amount).append(tip / 100 * amount).toString());
                    totalTip.setText(tip / 100 * amount);
                    // totalPerPerson.setText(totalBill.get/people);
                } catch (NumberFormatException ed) {
                    System.out.print("Not a number");
                }
            }
        });
//


    }
}

XML file content:

    <?xml version="1.0" encoding="utf-8"?>
    <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.btyagi.tipcalculator.MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Check Amount"
            android:id="@+id/textView"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="78dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="No. of People"
            android:id="@+id/textView2"
            android:layout_below="@+id/textView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="30dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Tip %"
            android:id="@+id/textView3"
            android:layout_below="@+id/textView2"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="29dp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Calculate"
            android:id="@+id/button"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/editText"
            android:layout_above="@+id/textView2"
            android:layout_toRightOf="@+id/textView"
            android:layout_toEndOf="@+id/textView" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/editText2"
            android:layout_above="@+id/textView3"
            android:layout_toRightOf="@+id/textView2"
            android:layout_toEndOf="@+id/textView2" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/editText3"
            android:layout_above="@+id/button"
            android:layout_toRightOf="@+id/textView"
            android:layout_toEndOf="@+id/textView" />

        <EditText
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/editText4"
            android:layout_below="@+id/button"
            android:layout_alignRight="@+id/editText2"
            android:layout_alignEnd="@+id/editText2" />

        <EditText
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/editText5"
            android:layout_below="@+id/editText4"
            android:layout_alignLeft="@+id/editText4"
            android:layout_alignStart="@+id/editText4" />

        <EditText
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/editText6"
            android:layout_below="@+id/editText5"
            android:layout_alignRight="@+id/editText"
            android:layout_alignEnd="@+id/editText" />

        <EditText
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/editText7"
            android:layout_below="@+id/editText6"
            android:layout_alignRight="@+id/editText6"
            android:layout_alignEnd="@+id/editText6" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="TipPerPerson"
            android:id="@+id/textView4"
            android:layout_alignBottom="@+id/editText7"
            android:layout_toRightOf="@+id/textView3"
            android:layout_toEndOf="@+id/textView3" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="TotalPerPerson"
            android:id="@+id/textView5"
            android:layout_alignBottom="@+id/editText6"
            android:layout_alignLeft="@+id/textView4"
            android:layout_alignStart
Yazan
  • 6,074
  • 1
  • 19
  • 33

2 Answers2

2

A final variable can only be initialized once.

Change your totalBill, totalTip and totalPerPerson TextViews (actually there are EditTexts in your xml) to class variable, and move your amount, people and tip to the click listener.

public class MainActivity extends AppCompatActivity {


   TextView totalBill;
   TextView totalTip;
   TextView totalPerPerson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        TextView checkAmount = (TextView) findViewById(R.id.textView);
        TextView noOfPeople = (TextView) findViewById(R.id.textView2);
        TextView tipPercent = (TextView) findViewById(R.id.textView3);

        totalBill = (TextView) findViewById(R.id.textView7);
        totalTip = (TextView) findViewById(R.id.textView6);
        totalPerPerson = (TextView) findViewById(R.id.textView5);
        TextView tipPerPerson = (TextView) findViewById(R.id.textView4);


        Button calculateButton = (Button) findViewById(R.id.button);

       calculateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try { 
                   int amount = Integer.parseInt(checkAmount.getText().toString());
                   int people = Integer.parseInt(noOfPeople.getText().toString());
                   int tip = Integer.parseInt(tipPercent.getText().toString());

                    totalBill.setText(new StringBuilder().append(amount).append(tip / 100 * amount).toString());
                    totalTip.setText(String.valueOf(tip / 100 * amount));
                    // totalPerPerson.setText(totalBill.get/people);
                } catch (NumberFormatException ex) {
                  System.out.print("Not a number");
                }
            }
        });

    }
}
Rami
  • 7,879
  • 12
  • 36
  • 66
  • Thanks a lot!! I forgot that i changed those views in my XML code later on. I believe your suggestions would solve my issue. – bhavishya tyagi Jan 24 '16 at 00:17
  • I have incorporated changes in the question,as you have suggested but the app crashes after i click calculate button. – bhavishya tyagi Jan 24 '16 at 00:39
  • 1
    You need to cast your int to a String in *setText()* method: *String.valueOf(yourInt)*. I've updated my answer. – Rami Jan 24 '16 at 07:57
0

This line

totalTip.setText(tip / 100 * amount);

Should say this instead

totalTip.setText(String.valueOf(tip / 100 * amount));

Reason being is that setText(int resId) is the wrong method and will throw a Resources$NotFoundException that you are getting.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245