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.
Code follows:
XML:
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.
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);
}
}
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>