• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Using an integer in TextView.setText() crashes my app

rminkler

Lurker
Sep 14, 2011
3
1
I've been trying to solve this issue for a while, but can't seem to figure out why this is crashing. I'm trying to do something really simple that is ending up being really hard. I'm new to Java and Android, so it may just be that I'm just doing something totally wrong and stupid without realizing it.
I'm trying to take numerical user input, do some math with it, and output the results. It seemed like it would be simple enough. When I run it, it crashes. If I change the setText() method to handle a String instead of an integer then it works perfectly. So I either need to figure out how to change the integer back into a string, or find out what I'm doing wrong in the first place. Please help this noob out.:thinking:

Code follows:

Code:
package test.math;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SimpleAppTestActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // create button listener
        Button button = (Button) findViewById(R.id.Submit);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        // get form data one
        final EditText one = (EditText) findViewById(R.id.number1);
        String oneStr = one.getText().toString();
        int oneInt = Integer.parseInt(oneStr); // convert String to Int

        // get form data two
        final EditText two = (EditText) findViewById(R.id.number2);
        String twoStr = two.getText().toString();
        int twoInt = Integer.parseInt(twoStr); // convert String to Int

        // get form data three
        final EditText three = (EditText) findViewById(R.id.number3);
        String threeStr = three.getText().toString();
        int threeInt = Integer.parseInt(threeStr); // convert String to Int

        // calculate results
        int results = oneInt + twoInt + threeInt;

        // Output results to TextView
        final TextView text = (TextView) findViewById(R.id.results);
        text.setText(results);
    }
}
XML:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="Censored to allow posting"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <EditText 
           android:id="@+id/number1"
           android:layout_width="150dp" 
           android:layout_height="wrap_content" 
           android:inputType="number" 
           android:layout_gravity="center"
           android:hint="Number 1"
           android:layout_centerHorizontal="true"
    >
        <requestFocus/>
    </EditText>
    <EditText             
        android:id="@+id/number2"
           android:layout_width="150dp" 
           android:layout_height="wrap_content" 
           android:inputType="number" 
           android:layout_gravity="center"
           android:hint="Number 2"
           android:layout_centerHorizontal="true" 
    />
    <EditText 
        android:id="@+id/number3"
           android:layout_width="150dp" 
           android:layout_height="wrap_content" 
           android:inputType="number" 
           android:layout_gravity="center"
           android:hint="Number 3"
           android:layout_centerHorizontal="true" 
    />

    <Button 
        android:layout_width="wrap_content" 
           android:text="Submit" 
           android:layout_height="wrap_content" 
           android:layout_gravity="center" 
           android:id="@+id/Submit" 
           android:clickable="true"
        android:layout_centerHorizontal="true"
    />
    <TextView 
        android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:textAppearance="?android:attr/textAppearanceLarge" 
           android:id="@+id/results" 
           android:text="Results" 
           android:layout_centerHorizontal="true"
    />

</LinearLayout>
 
Thanks jonbonazza.
You got me on the right path. I ended up using double instead of integers for my "real" project, but it works just the same using Double.toString(results);
(I created another simpler project to ask about the issue and to make sure it wasn't other code lurking around that was causing the crash.)

Thanks JiMMaR,
Shortcuts may work, but I'm still learning so I need my code to be as readable as possible right now so I understand what I did the next time I look at it. That said, it is still good to know other ways to do the same thing so if I see code that uses it I won't be totally stumped.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones