-3

So, when I press the Increment or Decrement button on my device, the app force closes. I tried using debugger but can't seem to figure out the problem. It is basically a single screen app that shows Order summary.

Here is the XML Code:

<ScrollView 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"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <TextView
            android:id="@+id/name_field"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="QUANTITY!" />


        <CheckBox
            android:id="@+id/whipped_cream_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="24dp"
            android:text="Whipped Cream"
            android:textSize="16sp" />



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:onClick="decrement"
                android:text="-" />

            <TextView
                android:id="@+id/quantity_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="8dp"
                android:paddingRight="8dp"
                android:text="0"
                android:textColor="@android:color/black"
                android:textSize="16sp" />

            <Button
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:onClick="increment"
                android:text="+" />

        </LinearLayout>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:onClick="submitOrder"
            android:text="Order" />

        <TextView
            android:id="@+id/order_summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="16dp"/>

    </LinearLayout>
</ScrollView>

The JAVA code:

    package com.example.android.timeforacoffee;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

    int quantity=0;
    String priceMessage="Name: Moksh \n" + "Your total is ₹" +quantity;

    public void displayQuanity(int quantity) {
        TextView quantity_value=(TextView) findViewById(R.id.quantity_text_view);
        quantity_value.setText(quantity);
    }

    /**
     *This method is called when the - button is pressed.
     */
    public void decrement(View view) {
        displayQuanity(quantity - 1);
    }

    /**
     *
     * This method is called when the + button is clicked.
     */
    public void increment(View view) {
        displayQuanity(quantity + 1);
    }


    public void displayMessage(String priceMessage) {
        TextView order_summary=(TextView) findViewById(R.id.order_summary);
                order_summary.setText(priceMessage);
    }

    /**
     * This method is called when the Order button is clicked.
     */

    public void submitOrder(View view) {
        displayMessage(priceMessage);
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    Do you get an error message in the logs? Look for `fatal` – vkislicins Nov 05 '16 at 14:54
  • 1
    post your logcat. – SRB Bans Nov 05 '16 at 14:56
  • 2
    Possible duplicate of [android.content.res.Resources$NotFoundException: String resource ID #0x0](http://stackoverflow.com/questions/20177003/android-content-res-resourcesnotfoundexception-string-resource-id-0x0) – Mike M. Nov 05 '16 at 14:58
  • 1
    it might not be immediately obvious why, but this is a duplicate of the linked question. There are a couple versions of `setText` (overloaded) - you are passing an integer which is then treated like a resource id. To fix it you need to convert `quantity` into a `String` before making the call to `setText` - either `quantity + ""` or use `String.valueOf`. – trooper Nov 05 '16 at 15:08

1 Answers1

0

I think the issue is in the below line of code

   quantity_value.setText(quantity);

Note that when you pass any integer to setText function android considers the integer as an reference to a string reource and searches for that string in the reource file, if that string is not found your app will crash with android.content.res.Resources$NotFoundException. You can simply fix this issue by explicitly parsing the integer as string as below

  quantity_value.setText(String.valueOf(quantity));
nvinayshetty
  • 3,187
  • 1
  • 21
  • 32
  • Thanks, mate. That solved my problem. But, now when I click the increment button, it just doesn't increase the value greater than 1 or in case of decrement button, -1. – Moksh Verma Nov 05 '16 at 16:05
  • learn some basics of programming...note you never changed the value of quantity – nvinayshetty Nov 05 '16 at 16:10
  • That's a really good advice. See, I am learning. Thanks to you. :) And I had figured it out before hand. – Moksh Verma Nov 05 '16 at 16:19