-1

I have created one class called SplashActivity.I have added two numbers on this activity .I want to show the answer on the next activity but it does not show there .I have created two EditText fields then a button. When the button is clicked new activity should start and the answer of that addition should be displayed .

EditText e=(EditText)findViewById(R.id.editText3);
    EditText e2=(EditText)findViewById(R.id.editText4);`
    int num1=Integer.parseInt(e.getText().toString());
    int num2=Integer.parseInt(e2.getText().toString());
    int sum=num1+num2;
    Intent in=new Intent(this,SecondSplash.class);
    in.putExtra("value",sum);
    startActivity(in);

And Its Xml is

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.sammar.firstapplication.SplashActivity"
    android:background="@drawable/background">

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="76dp"
        android:ems="10"
        android:inputType="numberSigned"
        android:hint="Enter First Number"/>

    <EditText
        android:id="@+id/editText4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:inputType="numberSigned"
        android:hint="Enter Second Number"
        android:layout_below="@+id/editText3"
        android:layout_alignStart="@+id/editText3" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="46dp"
        android:onClick="onButtonClick"
        android:text="ADD"
        android:layout_above="@+id/button6"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="51dp" />

And this is the code of SecondActivity. In this activity i have created a bundle which receives the data .

 TextView txtView;
    String value;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_splash);

        Bundle b=getIntent().getExtras();
        if(b!=null)
        {
            value=b.getString("value");
        }
        txtView =(TextView)findViewById(R.id.textView2);
        //value=getIntent().getExtras().getString("value");
        txtView.setText(value);

and its XML is

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.sammar.firstapplication.SecondSplash">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="132dp"
        android:layout_marginTop="191dp"
         />
</RelativeLayout>

I cannot get the value of sum on the second activity. Kindly helpenter code here.

Darush
  • 11,403
  • 9
  • 62
  • 60
Sammar Ahmad
  • 25
  • 1
  • 7

2 Answers2

0
value=b.getInt("value");

The type of sum is int and not String.

ILYAS_Kerbal
  • 1,369
  • 11
  • 18
0

put your intent inside onClick like,

button.setOnClickListner(new OnClickListner)
{
   @override OnClick()
   {
       Intent in=new Intent(this,SecondSplash.class);
       in.putExtra("value",String.valueOf(sum));
       startActivity(in);
   }
}

change your code

    Bundle b=getIntent().getExtras();
    if(b!=null)
    {
        value=b.getString("value");
    }

to

 String value = getIntent().getStringExtra("value");
Mahesh Gawhane
  • 348
  • 3
  • 20