0

hi
i want to create a simple android app that gets two numbers in the main activity and when we click submit it goes to another activity and displays the result.

i created two classes

mainactivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText et1=(EditText) findViewById(R.id.editText1);
        EditText et2=(EditText) findViewById(R.id.editText2);
        Button b=(Button) findViewById(R.id.button1);


        final int result=Integer.parseInt(et1)+Integer.parseInt(et2);

        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent=new Intent(MainActivity.this,Result.class);
                intent.putExtra("Result", result);
                startActivity(intent);

            }
        });
    }

Result.java

public class Result extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        TextView tv=(TextView) findViewById(R.id.textView1);
        tv.setText(getIntent().getExtras().getInt("Result"));
    }

i am getting error. the apps asks to force close. please help me

3
Contributors
2
Replies
9
Views
5 Years
Discussion Span
Last Post by peter_budo
0

The problem is that you have to use Integer.toString() to convert your int "extra" to a String type before you can use TextView.setText(). Line 9 of Result.java should read:

        tv.setText(Integer.toString(getIntent().getExtras().getInt("Result")));

Hope that helps! Happy coding!

Edited by BitBlt

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.