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.