When I place a main screen, this main screen turns off the calculator on the second screen [duplicate]

-1

I made the App to add two numbers, but when I put a start screen with just one button to call the sum screen, the calculations of the sum screen are inactive, how to solve?

MainActivity.java screen

package br.com.frutasalmeidasantos.www.testefinal;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    public void chamasolucao (View v) { setContentView(R.layout.activity_solucao); }
}

Soluction.java

package br.com.frutasalmeidasantos.www.testefinal;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Solucao extends AppCompatActivity {
    EditText campo1;
    EditText campo2;
    Button btcalc;
    TextView resposta;



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

        campo1 = (EditText) findViewById(R.id.campo1);
        campo2 = (EditText) findViewById(R.id.campo2);
        btcalc = (Button) findViewById(R.id.btcalc);
        resposta = (TextView) findViewById(R.id.resposta);

        btcalc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String scampo1 = campo1.getText().toString();
                String scampo2 = campo2.getText().toString();

                if (scampo1.isEmpty() || scampo2.isEmpty()){
                    Toast.makeText(Solucao.this, "", Toast.LENGTH_SHORT).show();
                    return;
                }

                int ncampo1 = Integer.parseInt(scampo1);
                int ncampo2 = Integer.parseInt(scampo2);

                int nresposta = ncampo1 + ncampo2;

                resposta.setText(""+ncampo1+ "+" +ncampo2+ "=" +nresposta);
            }
        });
    }
}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.com.frutasalmeidasantos.www.testefinal">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Solucao"></activity>
    </application>

</manifest>
    
asked by anonymous 09.04.2017 / 19:16

1 answer

-1

To solve your problem, do the following:

At line public class MainActivity extends AppCompatActivity { add implements View.OnClickListener leaving the line as follows:

public class MainMenuActivity extends AppCompatActivity implements View.OnClickListener{

Below onCreate you create method public void onClick(View v){}

The result will be ( MainActivity ):

package br.com.frutasalmeidasantos.www.testefinal;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainMenuActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btnSolucao;

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

        btnSolucao=(Button)findViewById(R.id.btnSolucao);
        btnSolucao.setOnClickListener(this);

    }

    public void onClick(View v){

        Intent it = new Intent(this, Solucao.class);
        startActivity(it);

    }

}
    
09.04.2017 / 21:03