Android Studio: Invisible problem?

Asked

Viewed 139 times

0

Hi, I’m making an app to calculate the best purchase between 2 products, in this case is toilet paper.

In the title I put invisible problem because android studio 3 does not detect any error in the build, however, when I run the application, when I click on the button compare the application gives error and asks to be restarted, without explaining where is the error. I leave here the codes to see if someone can help me because I can not find the error, nor android studio, lool

Xml file:

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/textView2"
    android:layout_width="420dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="24dp"
    android:layout_marginStart="24dp"
    android:layout_marginTop="28dp"
    android:text="Comparar 2 preços de papel higienico para saber qual é o mais barato. Preencha os dados e clique no botão Comparar!"
    android:textSize="18sp" />

<EditText
    android:id="@+id/editPrecoA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_alignStart="@+id/textView2"
    android:layout_below="@+id/textView3"
    android:layout_marginTop="19dp"
    android:ems="10"
    android:hint="Preço"
    android:inputType="numberDecimal"
    android:textSize="14sp" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editPrecoA"
    android:layout_alignStart="@+id/editPrecoA"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="41dp"
    android:text="Papel Higiénico A"
    android:textSize="18sp" />

<EditText
    android:id="@+id/editRolosA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/editPrecoA"
    android:layout_alignLeft="@+id/editPrecoA"
    android:layout_alignRight="@+id/editPrecoA"
    android:layout_alignStart="@+id/editPrecoA"
    android:layout_below="@+id/editPrecoA"
    android:ems="10"
    android:hint="Nº Rolos"
    android:inputType="number"
    android:textSize="14sp" />

<EditText
    android:id="@+id/editMetrosA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/editRolosA"
    android:layout_alignLeft="@+id/editRolosA"
    android:layout_alignRight="@+id/editRolosA"
    android:layout_alignStart="@+id/editRolosA"
    android:layout_below="@+id/editRolosA"
    android:ems="10"
    android:hint="Metros por rolo"
    android:inputType="number"
    android:textSize="14sp" />

<EditText
    android:id="@+id/editPrecoB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/editPrecoA"
    android:layout_alignBottom="@+id/editPrecoA"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_marginEnd="21dp"
    android:layout_marginRight="21dp"
    android:ems="10"
    android:hint="Preço"
    android:inputType="numberDecimal"
    android:textSize="14sp" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView3"
    android:layout_alignBottom="@+id/textView3"
    android:layout_alignLeft="@+id/editPrecoB"
    android:layout_alignStart="@+id/editPrecoB"
    android:text="Papel Higiénico B"
    android:textSize="18sp" />

<EditText
    android:id="@+id/editRolosB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editPrecoB"
    android:layout_alignStart="@+id/editPrecoB"
    android:layout_below="@+id/editPrecoB"
    android:ems="10"
    android:hint="Nº Rolos"
    android:inputType="number"
    android:textSize="14sp" />

<EditText
    android:id="@+id/editMetrosB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editRolosB"
    android:layout_alignStart="@+id/editRolosB"
    android:layout_below="@+id/editRolosB"
    android:ems="10"
    android:hint="Metros por rolo"
    android:inputType="number"
    android:textSize="14sp" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textResultadoB"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:onClick="calcular_produtos"
    android:text="Comparar" />

<TextView
    android:id="@+id/textResultadoA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editMetrosA"
    android:layout_alignStart="@+id/editMetrosA"
    android:layout_centerVertical="true"
    android:text="....." />

<TextView
    android:id="@+id/textResultadoB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editMetrosB"
    android:layout_alignStart="@+id/editMetrosB"
    android:layout_alignTop="@+id/textResultadoA"
    android:text="....." />

<TextView
    android:id="@+id/textResultadoFinal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/textView3"
    android:layout_alignRight="@+id/textView3"
    android:layout_below="@+id/button"
    android:layout_marginEnd="27dp"
    android:layout_marginRight="27dp"
    android:layout_marginTop="69dp"
    android:text="....." />

Now the class file

package com.blogspot.comprasinteligentes;

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;

public class Main2Activity extends AppCompatActivity {

    EditText editPrecoA, editPrecoB, editRolosA, editRolosB, editMetrosA, editMetrosB;
    TextView textResultadoA, textResultadoB, textResultadoFinal;
    Button button;

    Double NumRolosA, NumRolosB, TotalMetrosA, TotalMetrosB, NumMetrosA, NumMetrosB, ValorPorMetroA, ValorPorMetroB, PrecoA, PrecoB, Dif, Poupanca;

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


        //Procura os valores dos objetos
        editPrecoA = (EditText)findViewById(R.id.editPrecoA);
        editPrecoB = (EditText)findViewById(R.id.editPrecoB);

        editRolosA = (EditText)findViewById(R.id.editRolosA);
        editRolosB = (EditText)findViewById(R.id.editRolosB);

        editMetrosA = (EditText)findViewById(R.id.editMetrosA);
        editMetrosB = (EditText)findViewById(R.id.editMetrosB);

        textResultadoA = (TextView)findViewById(R.id.textResultadoA);
        textResultadoA = (TextView)findViewById(R.id.textResultadoB);
        textResultadoFinal = (TextView)findViewById(R.id.textResultadoFinal);

        button = (Button)findViewById(R.id.button);


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            //Ao clicar em Comparar então vai calcular o produto mais barato
             // Contas ==========================================
             // TotalMetros = NºRolos x Metros por rolo
             // ValorPorMetro = Preco / TotalMetros

             // Dif = ValorPorMetroA - ValorPorMetroB

             // Poupança = Did x TotalMetros

             //================================================

                NumRolosA = Double.parseDouble(editRolosA.getText().toString());
                NumMetrosA = Double.parseDouble(editMetrosA.getText().toString());

                TotalMetrosA = NumRolosA * NumMetrosA;

                NumRolosB = Double.parseDouble(editRolosB.getText().toString());
                NumMetrosB = Double.parseDouble(editMetrosB.getText().toString());

                TotalMetrosB = NumRolosB * NumMetrosB;

               PrecoA = Double.parseDouble(editPrecoA.getText().toString());
               ValorPorMetroA = PrecoA / TotalMetrosA;

                PrecoB = Double.parseDouble(editPrecoB.getText().toString());
                ValorPorMetroB = PrecoB / TotalMetrosB;



             if (ValorPorMetroA > ValorPorMetroB) {
                 Dif = ValorPorMetroA - ValorPorMetroB;
                 Poupanca = Dif * TotalMetrosB;

                 //Mostra resultados
                 textResultadoFinal.setText("Vencedor: Papel Higiénico A\nPoupa nesta embalagem: " + String.valueOf(Poupanca));
             }

             if (ValorPorMetroA < ValorPorMetroB) {
                 Dif = ValorPorMetroB - ValorPorMetroA;
                 Poupanca = Dif * TotalMetrosA;

                 //Mostra resultado
                 textResultadoFinal.setText("Vencedor: Papel Higiénico B\nPoupa nesta embalagem: " + String.valueOf(Poupanca));
             }

             if (ValorPorMetroA == ValorPorMetroB) {
                 Dif = ValorPorMetroA - ValorPorMetroB;
                 Poupanca = Dif * TotalMetrosA;

                 textResultadoFinal.setText("Vencedor: Ambos custam o mesmo!");
             }

             textResultadoA.setText("Preço por metro: " + String.valueOf(ValorPorMetroA));
             textResultadoB.setText("Preço por metro: " + String.valueOf(ValorPorMetroB));
            }

     });

    }
  • Dude, it’s IMPOSSIBLE not to have a Logcat with the bug, but I believe in Textview with ID editPrecoA has a android:layout_below="@+id/textView3" whereas the textView3 hasn’t even been created yet

  • Hello, I passed editPrecoA down from textView3, however it does not work. The program says it suffered a system failure.

  • 1

    Hello! see Logcat error see this link

1 answer

3


You made a mistake here:

textResultadoA = (TextView)findViewById(R.id.textResultadoA);
textResultadoA = (TextView)findViewById(R.id.textResultadoB);
textResultadoFinal = (TextView)findViewById(R.id.textResultadoFinal);

Has 2 textResultadoA

Correct:

textResultadoA = (TextView)findViewById(R.id.textResultadoA);
textResultadoB = (TextView)findViewById(R.id.textResultadoB);
textResultadoFinal = (TextView)findViewById(R.id.textResultadoFinal);
  • Great Friend! It works! You discovered the needle in the haystack! Thanks for seeing this flaw that escaped me, I don’t know how (maybe from copy and paste).

  • Oops, we’re there ! Don’t forget to see the way the ramaral went !

  • My answer was wrong.

  • Was ? You just passed how to use with public instead of do on onCreate was not ?

  • @Celestinoxp don’t forget to mark as resolved.

  • My answer was based on the wrong principle that if the attribute android:onClick is used it has to be implemented. What is not true if you use the setOnClickListener().

  • Ah understood... is that I usually use everything the maximum outside of onCreate, so that I can manipulate the properties, actions, etc of the elements.

  • Oops ! I’ll see ! Thanks @ramaral ! Thank you

  • @Raonibz how do I mark as resolved?

  • Next to the answer is a "V". Click on it that will turn green.

Show 6 more comments

Browser other questions tagged

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