Android App. Simple Calculator Crashing

-1

I'm new to android development and I ran into a snag on my first real attempt and an app. I'm trying to make a simple calculator. However when I run the app and enter my the second number in the sequence to be added the calculator the emulator says "Unfortunately the calculator has stopped working." I looked at the logCat and there are a tun of red errors so I don't think I'll post all of them.

My code is as fallows:

    package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.content.Context;
import android.view.View;


public class HelloAndroidActivity extends Activity {
    EditText helloName;
    /** Called when the activity is first created. */

    int value = 0;
    int acuum = 0;
    boolean newValue = false;

    Button add, sub, mult, div, equ, one, two, three;
    TextView display;

    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        add = (Button) findViewById(R.id.Add);
        sub = (Button) findViewById(R.id.Sub);
        mult= (Button) findViewById(R.id.Mult);
        div = (Button) findViewById(R.id.Div);
        equ = (Button) findViewById(R.id.Equ);   
        one = (Button) findViewById(R.id.One); 
        two = (Button) findViewById(R.id.Two); 
        three = (Button) findViewById(R.id.Three); 
        display = (TextView) findViewById(R.id.Value);

        add.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                value = Integer.parseInt(display.getText().toString());
                acuum += value;
                newValue = true;
            }
        });

        sub.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                value = Integer.parseInt(display.getText().toString());
                acuum -= value;
                newValue = true;

            }
        });

        one.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if(newValue == true)
                    display.setText('1');
                else
                    display.setText(display.getText().toString() +  '1');

                newValue = false;

            }
        });

        two.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if(newValue == true)
                    display.setText('2');
                else
                    display.setText(display.getText().toString() +  '2');

                newValue = false;

            }
        });

    }

}

Just to try and further clarify. If I were to enter 1 + 2, when I click the 2 is when the app crashes. I feel like its a simple mistake that I'm just over looking. Any help is greatly appreciated.

Thank in advance!

LogCat Value:

05-15 02:39:30.047: D/gralloc_goldfish(540): Emulator without GPU emulation detected. 05-15 02:39:30.097: I/dalvikvm(540): threadid=3: reacting to signal 3 05-15 02:39:30.128: I/dalvikvm(540): Wrote stack traces to '/data/anr/traces.txt' 05-15 02:39:46.427: W/ResourceType(540): No package identifier when getting value for resource number 0x00000032 05-15 02:39:46.427: D/AndroidRuntime(540): Shutting down VM 05-15 02:39:46.427: W/dalvikvm(540): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 05-15 02:39:46.437: E/AndroidRuntime(540): FATAL EXCEPTION: main 05-15 02:39:46.437: E/AndroidRuntime(540): android.content.res.Resources$NotFoundException: String resource ID #0x32 05-15 02:39:46.437: E/AndroidRuntime(540): at android.content.res.Resources.getText(Resources.java:247) 05-15 02:39:46.437: E/AndroidRuntime(540): at android.widget.TextView.setText(TextView.java:3473) 05-15 02:39:46.437: E/AndroidRuntime(540): at com.example.helloandroid.HelloAndroidActivity$4.onClick(HelloAndroidActivity.java:80) 05-15 02:39:46.437: E/AndroidRuntime(540): at android.view.View.performClick(View.java:3511) 05-15 02:39:46.437: E/AndroidRuntime(540): at android.view.View$PerformClick.run(View.java:14105) 05-15 02:39:46.437: E/AndroidRuntime(540): at android.os.Handler.handleCallback(Handler.java:605) 05-15 02:39:46.437: E/AndroidRuntime(540): at android.os.Handler.dispatchMessage(Handler.java:92) 05-15 02:39:46.437: E/AndroidRuntime(540): at android.os.Looper.loop(Looper.java:137) 05-15 02:39:46.437: E/AndroidRuntime(540): at android.app.ActivityThread.main(ActivityThread.java:4424) 05-15 02:39:46.437: E/AndroidRuntime(540): at java.lang.reflect.Method.invokeNative(Native Method) 05-15 02:39:46.437: E/AndroidRuntime(540): at java.lang.reflect.Method.invoke(Method.java:511) 05-15 02:39:46.437: E/AndroidRuntime(540): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 05-15 02:39:46.437: E/AndroidRuntime(540): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-15 02:39:46.437: E/AndroidRuntime(540): at dalvik.system.NativeStart.main(Native Method) 05-15 02:39:47.077: I/dalvikvm(540): threadid=3: reacting to signal 3 05-15 02:39:47.097: I/dalvikvm(540): Wrote stack traces to '/data/anr/traces.txt'

Sorry about the formatting of the logCat.

java
android
asked on Stack Overflow May 15, 2012 by Ed Pizzella • edited May 15, 2012 by Ed Pizzella

1 Answer

1

A few things you may want to look at.

You use the character '2' in a few places where it might make more sense to use the string "2". For example:

display.setText('1');

and:

display.setText(display.getText().toString() +  '2');

I think it's that first one causing your specific problem. The support for this lies in the following error from your error log:

FATAL EXCEPTION: main 05-15 02:39:46.437: E/AndroidRuntime(540):
android.content.res.Resources$NotFoundException:
String resource ID #0x32

#0x32 (hex 32) is the integral value of '2'. If you look at the TextView documentation, you'll see a few prototypes for setText, the first which takes an integral resource ID:

public final void setText (int resid)

which explains what's happening. The '2' is being treated as an integer and used to look up a resource (which doesn't exist).


It might also be a good idea to initialise newValue to true rather than false, since that should be the initial state. Otherwise, if your calculator starts with "0" in the display, pressing the 1 button would give you 01 rather than 1.

answered on Stack Overflow May 15, 2012 by paxdiablo • edited May 15, 2012 by paxdiablo

User contributions licensed under CC BY-SA 3.0