Pass parameter to second activity [closed]

1

I'm trying to pass some parameter to another activity, but the application hangs on the second screen. The name of the application is calculator. The error that says on the second screen is: Calculator has stopped.

Here are the codes:

xml of main activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical"  
    tools:context="${relativePackage}.${activityClass}">  

    <TextView 
        android:id="@+id/tx1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
    android:text="valor 1"/>  

    <EditText  
        android:id="@+id/edt1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:inputType="numberDecimal"/>  

    <TextView  
        android:id="@+id/tx2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="valor 2"/>  

    <EditText  
        android:id="@+id/edt2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:inputType="numberDecimal"/>  

    <Button  
        android:id="@+id/btn1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="calcular"/>  

</LinearLayout>  

Activity class:

package com.estudos.calculadora;  

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 MainActivity extends Activity {  

@Override  
protected void onCreate(Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);  
setContentView(R.layout.activity_main);  
final TextView tx1 = (TextView)findViewById(R.id.tx1);  

final EditText edt1 =(EditText)findViewById(R.id.edt1);  

TextView tx2 = (TextView)findViewById(R.id.tx2);  

final EditText edt2 =(EditText)findViewById(R.id.edt2);  

Button calcular =(Button)findViewById(R.id.btn1);  

calcular.setOnClickListener(new OnClickListener() {  

@Override  
public void onClick(View v) {  
double valor1=Double.parseDouble(edt1.getText().toString());  
double valor2=Double.parseDouble(edt2.getText().toString());  
Double soma=valor1+valor2;  

Intent intent = new Intent(MainActivity.this, Activity_tela2.class);  

intent.putExtras("soma", soma);  

        startActivity(intent);  
}  
});  
}  

}  

xml of the second acitivy:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical"  
    tools:context="${relativePackage}.${activityClass}">  

    <TextView  
        android:id="@+id/resultado"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="resultado"/>  

    <Button  
        android:id="@+id/vtn_volta"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="voltar"/>  

</LinearLayout>  

class code:

package com.estudos.calculadora;  

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.TextView;  

public class Activity_tela2 extends Activity {  

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

Intent intent = getIntent();  

TextView resultado =(TextView)findViewById(R.id.resultado);  

Button btn_volta =(Button)findViewById(R.id.btn_volta);  
}  

}  

I have not dealt with the onclick of the back button yet. I just want to understand why I still can not get to the second screen.

    
asked by anonymous 09.10.2016 / 00:35

3 answers

-1

When creating Intent do so:

Intent i = new Intent(v.getContext(), Classe_Tela2.class); i.putExtra("soma", soma); startActivity(i);

And in the second activity, do the following:

Bundle receptor = getActivity().getExtras(); //depois é só receber os valores usando Bundle (receptor)

    
09.10.2016 / 22:01
-2

You missed this line friend:

Intent intent = new Intent(MainActivity.this, Activity_tela2.class);

It should look like this:

Intent intent = new Intent();
intent.setClass(MainActivity.this, Activity_tela2.class);
startActivity(intent);

That is, you first create a new intent with the new Intent() method, then use the setClass method to define the current Activity and which Activity you want to go to.

    
09.10.2016 / 16:17
-2

In Java, "Value is different from Value" and "VaLoR is different from VALUE" . In this line where you declare the sum variable, you put Double , try putting double .

Type: *double* soma = valor1 + valor2; And not *Double* soma = valor1 + valor2;

    
09.10.2016 / 21:47