When I place a main screen, this main screen disables the calculator on the second screen

Asked

Viewed 77 times

-1

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

Screen Mainactivity.java

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); }
}

Solucao.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>
  • Change the method chamasolucao() for public void chamasolucao (View v) { startActivity(new Intent(this, Solucao.class)); }

  • 1

    Thanks @ramaral, it worked!

1 answer

-1

To solve your problem do the following:

On the line public class MainActivity extends AppCompatActivity { add implements View.OnClickListener leaving the line that way:

public class MainMenuActivity extends AppCompatActivity implements View.OnClickListener{

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

The result will be this (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);

    }

}
  • I did exactly as you suggested, it still didn’t work! Any suggestions?

  • @Gilbertoalmeida vc nn creates a new method, you put the Mplements in your public class before onCreate

  • Good evening! Can you send me the example as in the model above? I’m beginner and although I’ve come this far, now I’ve stopped! I can run the splash screen and the separate calculator for example, but I cannot make the calculator work when I have two activities, it gets the boot calculate null.

  • Please do not use snippet if it is not an executable code on the site, such as javascript, css and html.

  • @diegofm ??? nn got it

Browser other questions tagged

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