Beginner with Android/Java - help with Intent and adding two numbers
i've been working on this for several days and i'm just out of ideas. i've searched everywhere and have the book ProAndroid2.
i'm trying to do something simple (i thought) which was have two text boxes where a user enters a number in each, then pushes a button and the sum of the numbers will display.
i found this on stackoverflow about doing pretty much the exact same thing and have been doing pretty good at learning all sorts of stuff, but i'm apparently missing something very very basic. i can get the text view to display a sum if i set the variables, but i can't figure out how to get user input.
the errors i'm getting are that EditText can't be parsed as in integer, even tho everything i find says to use the toString method i'm using. and i think it might be becaus开发者_如何学运维e i'm not sure how to get the app to wait to parse it until the button is clicked which is what i thought i was doing with how i have it set up. so essentially it's parsing nothing and of course that can't be parsed. ugh.
any help much appreciated. please believe i've searched and searched and read and read, i think i might be missing out on some important concept and not know what exactly to search for to get help. i've done some other tutorials but this is just really making me crazy.
import android.app.Activity;
import android.content.Intent;
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 AddNumbers extends Activity
{
TextView textview;
Button button1;
EditText number1, number2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textview = (TextView)findViewById(R.id.textView);
//gets numbers from user
number1 = new EditText(this);
number2 = new EditText(this);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(btnListener);
}
private OnClickListener btnListener = new OnClickListener()
{
public void onClick(View v)
{
int int1 = Integer.parseInt(number1.getText().toString());
int int2 = Integer.parseInt(number2.getText().toString());
int sum = int1 + int2;
Intent intent = new Intent(this,AddNumbers.class);
intent.putExtra("sum",sum);
startActivity(intent);
getIntent();
intent.getIntExtra("sum", sum);
textview.setText(String.valueOf(sum));
}
}
};
and here's my xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:layout_width="match_parent" android:text="EditText"
android:layout_height="wrap_content" android:id="@+id/number1"></EditText>
<EditText android:layout_width="match_parent" android:text="EditText"
android:layout_height="wrap_content" android:id="@+id/number2"></EditText>
<Button android:id="@+id/button1" android:text="Button" android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView android:text="TextView" android:id="@+id/textView"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
and the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.self.projectOne"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="AddNumbers"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
you should get your editTexts from the content of your activity like this :
//gets numbers from user
number1 = (EditText)findViewById(R.id.number1);
number2 = (EditText)findViewById(R.id.number2);
and then on your onClick method: you dont need to start your activity because you are already on it: try this :
@Override
public void onClick(View v)
{
try{
int int1 = Integer.parseInt(number1.getText().toString().trim());//Edited line
int int2 = Integer.parseInt(number2.getText().toString().trim());//edited line
}catch(NumberFormatEception e){
Toast.makeText(this,"try to enter a valid number", 3000).show();
}
int sum = int1 + int2;
//display the sum on the textView
textview.setText(String.valueOf(sum));
}
You haven't mapped the Java EditText
to the one in the XML. Assign number1 = (EditText) findViewById(R.id.number1);
to pick it up off of the XML layout.
精彩评论