-3

Please tell me why my app is not working I have made a coffee calculator app. Here is the xml code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="@string/disp"
        android:textColor="@android:color/black"
        android:textSize="20sp"
        android:textStyle="bold" />

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

        <Button
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:onClick="Add"
            android:text="@string/add"
            android:textColor="@android:color/black"
            android:textSize="30sp"
            android:textStyle="bold"
            android:background="#E57373" />

        <TextView
            android:id="@+id/sum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="@string/text"
            android:textColor="@android:color/black"
            android:textSize="20sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/sub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:onClick="Sub"
            android:text="@string/sub"
            android:textColor="@android:color/black"
            android:textSize="30sp"
            android:textStyle="bold"
            android:background="#E57373" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="@string/price"
        android:textColor="@android:color/black"
        android:textSize="20sp"
        android:textStyle="bold" />

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:text="@string/total"
            android:textColor="@android:color/black"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/res"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="@string/res"
            android:textColor="@android:color/black"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>

    <Button
        android:id="@+id/order"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="210sp"
        android:onClick="Order"
        android:text="@string/order"
        android:textColor="@android:color/black"
        android:textSize="20sp"
        android:textStyle="bold" 
        android:textAllCaps="true"
        android:background="#E57373"/>

</LinearLayout>

And this is my Java code

package com.example.extra;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

    public int num=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void Add(View view){
        //On click method for Add button
        TextView text=(TextView)findViewById(R.id.sum);
        num=num+1;
        text.setText(num);
    }

    public void Sub(View view){
        //On click method for Sub button
        TextView text=(TextView)findViewById(R.id.sum);
        if(num==0)
            text.setText(0);
        else
        {   num=num-1;
            text.setText(num);
        }   
    }

    public void Order(View view){
        //On click method for button ORDER
        TextView text=(TextView)findViewById(R.id.res);
        text.setText("$" + num*10);
    }

}

When I am launching the app and clicking the add button or sub button then the app crashes.But the ORDER button is working fine.Please help...

Nitish Chopra
  • 273
  • 1
  • 4
  • 11

1 Answers1

1

The reason your app is crashing is because of the int you are putting in your TextViews. setText() takes a string variable or an int specifying a resource Id. In this case the int is not specifying a resource ID so it is failing. Change the setText() in Sub and Add to this:

text.setText("" + num);
mattfred
  • 2,689
  • 1
  • 22
  • 38