1. Download our Official Android App: Forums for Android!

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

Discussion in 'Android Development' started by rminkler, Sep 14, 2011.

  1. rminkler

    rminkler Lurker
    Thread Starter
    Rank:
    None
    Points:
    6
    Posts:
    3
    Joined:
    Sep 14, 2011

    Sep 14, 2011
    3
    1
    6
    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 (Text):
    1. package test.math;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.view.View.OnClickListener;
    7. import android.widget.Button;
    8. import android.widget.EditText;
    9. import android.widget.TextView;
    10.  
    11. public class SimpleAppTestActivity extends Activity implements OnClickListener {
    12.     /** Called when the activity is first created. */
    13.     @Override
    14.     public void onCreate(Bundle savedInstanceState) {
    15.         super.onCreate(savedInstanceState);
    16.         setContentView(R.layout.main);
    17.  
    18.         // create button listener
    19.         Button button = (Button) findViewById(R.id.Submit);
    20.         button.setOnClickListener(this);
    21.     }
    22.  
    23.     @Override
    24.     public void onClick(View v) {
    25.  
    26.         // get form data one
    27.         final EditText one = (EditText) findViewById(R.id.number1);
    28.         String oneStr = one.getText().toString();
    29.         int oneInt = Integer.parseInt(oneStr); // convert String to Int
    30.  
    31.         // get form data two
    32.         final EditText two = (EditText) findViewById(R.id.number2);
    33.         String twoStr = two.getText().toString();
    34.         int twoInt = Integer.parseInt(twoStr); // convert String to Int
    35.  
    36.         // get form data three
    37.         final EditText three = (EditText) findViewById(R.id.number3);
    38.         String threeStr = three.getText().toString();
    39.         int threeInt = Integer.parseInt(threeStr); // convert String to Int
    40.  
    41.         // calculate results
    42.         int results = oneInt + twoInt + threeInt;
    43.  
    44.         // Output results to TextView
    45.         final TextView text = (TextView) findViewById(R.id.results);
    46.         text.setText(results);
    47.     }
    48. }
    XML:
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="Censored to allow posting"
    3.     android:orientation="vertical" android:layout_width="fill_parent"
    4.     android:layout_height="fill_parent">
    5.     <EditText
    6.            android:id="@+id/number1"
    7.            android:layout_width="150dp"
    8.            android:layout_height="wrap_content"
    9.            android:inputType="number"
    10.            android:layout_gravity="center"
    11.            android:hint="Number 1"
    12.            android:layout_centerHorizontal="true"
    13.     >
    14.         <requestFocus/>
    15.     </EditText>
    16.     <EditText            
    17.         android:id="@+id/number2"
    18.            android:layout_width="150dp"
    19.            android:layout_height="wrap_content"
    20.            android:inputType="number"
    21.            android:layout_gravity="center"
    22.            android:hint="Number 2"
    23.            android:layout_centerHorizontal="true"
    24.     />
    25.     <EditText
    26.         android:id="@+id/number3"
    27.            android:layout_width="150dp"
    28.            android:layout_height="wrap_content"
    29.            android:inputType="number"
    30.            android:layout_gravity="center"
    31.            android:hint="Number 3"
    32.            android:layout_centerHorizontal="true"
    33.     />
    34.  
    35.     <Button
    36.         android:layout_width="wrap_content"
    37.            android:text="Submit"
    38.            android:layout_height="wrap_content"
    39.            android:layout_gravity="center"
    40.            android:id="@+id/Submit"
    41.            android:clickable="true"
    42.         android:layout_centerHorizontal="true"
    43.     />
    44.     <TextView
    45.         android:layout_width="wrap_content"
    46.            android:layout_height="wrap_content"
    47.            android:textAppearance="?android:attr/textAppearanceLarge"
    48.            android:id="@+id/results"
    49.            android:text="Results"
    50.            android:layout_centerHorizontal="true"
    51.     />
    52.  
    53. </LinearLayout>
     

    Advertisement

  2. jonbonazza

    jonbonazza Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
    That shouldn't even compile, let alone execute.

    The reason for this is because the setText() method takes as a parameter a String instance.

    If you want to set the Text to the value of results, you would do this:
    Code (Text):
    1.  
    2. text.setText(Integer.toString(results));
    3.  
     
  3. JiMMaR

    JiMMaR Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    363
    Joined:
    Jun 12, 2010

    Jun 12, 2010
    363
    57
    53
    another way to do it is
    Code (Text):
    1.  
    2. text.setText(""+results);
    3.  
     
  4. jonbonazza

    jonbonazza Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
    Yes, but it is very poor practice to do this and is generally only done by new, lazy developers.

    Another "accepted" approach is to use String.valueOf() instead of Integer.toString().
     
    JiMMaR likes this.
  5. rminkler

    rminkler Lurker
    Thread Starter
    Rank:
    None
    Points:
    6
    Posts:
    3
    Joined:
    Sep 14, 2011

    Sep 14, 2011
    3
    1
    6
    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.
     
  6. swiftace

    swiftace Lurker
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Sep 28, 2011

    Sep 28, 2011
    1
    0
    5
    that will be regarded as "public final void setText (int resid)",so ,an invalid string id.
     

Share This Page

Loading...