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

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.