Pass parameter to the second Activity

Asked

Viewed 705 times

1

I am 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 stopped.

Follow the codes:

xml of the 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>  

Class of the Activity:

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 still haven’t treated the onclick of the back button. I just want to understand why I still can’t go to the second screen.

  • Apparently in the layout of the second Activity instead of btn_volta is declared vtn_volta.

  • Edit the question and enter the bug log.

3 answers

-1

When creating Intent do so:

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

And on Monday, do the following:

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

  • What is this v and Classe_Tela2?

  • Classe_tela2 is a reference I used for the second Activity. E v and the method parameter onClick(View v) But for the explanation I received, this has no relevance whatsoever!

  • 1

    What you propose is identical to what the AP is doing. Without knowing what the error is, any answer is "kick".

-2

You got that line wrong, buddy:

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

It must be so:

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

That is, first Voce creates a new Intent with the method new Intent(), only then use the method setClass to set the current Activity and which Activity Voce you want to go to.

  • 2

    This does not solve the problem, these two forms are equivalents.

-2

In Java, "value is different from Value" and "Value is different from VALUE" . On that line where you declare the sum variable, you put Double, try to put double.

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

  • 1

    If it was a syntax or type problem it was detected in the compilation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.