How to set a Textview with a DOUBLE variable?

Asked

Viewed 4,385 times

0

I’m trying to set the variable txtMedia1 with the value of the variable media1. I did so, but the Android Studio indicates that I have to change txtMedia1 to double or media1 for TextView:

package com.app.jeffersonalencar.composicao;

import android.renderscript.Double2;
import android.renderscript.Sampler;
import android.support.annotation.StringDef;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;

public class act_mains extends AppCompatActivity implements View.OnClickListener{

    //Declarando variáveis de coleta de medida
    public EditText edtTxt1, edtTxt2, edtTxt3;
    //Declarando campo de visualização da média por parte
    public TextView txtMedia1;
    //Declarando botão
    public Button btnMedia1;


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

        //Setando os valores dos campos de coleta para as variáveis
        edtTxt1 = (EditText) findViewById(R.id.edtTxt1);
        edtTxt2 = (EditText) findViewById(R.id.edtTxt2);
        edtTxt3 = (EditText) findViewById(R.id.edtTxt3);


    }

    public void onClick(View V){
        double medida1 = Double.parseDouble(edtTxt1.getText().toString());
        double medida2 = Double.parseDouble(edtTxt2.getText().toString());
        double medida3 = Double.parseDouble(edtTxt3.getText().toString());


        double media1 = (medida1 + medida2 + medida3)/3;

        //Terminar a briga e colocar o valor no TextView
        String media2 = String.valuesOf(media1);
        txtMedia1 = (TextView) getText(R.id.media2) ;

    }


}

Realizing what the Android Studio still asks yes of error.

  • Now after pressing the calculation button the application closes.. @Ack Lay public void onClick(View V){ double medida1 = Double.parseDouble(edtTxt1.gettext(). toString(); double medida2 = Double.parseDouble(edtTxt2.gettext(). toString(); double medida3 = Double.parseDouble(edtTxt3.gettext(). toString(); double Bicipalmedia = (measure1 + measure2 + measure3)/3; //End the fight and put the value in Textview String valueDouble= Double.toString(Bicipalmedia); txtMedia1.setText(valueDouble);

2 answers

1

I use a similar conversion in an app that has developed. Suitable for your example, would be as follows:

txtMedia.setText(Double.toString(media1));

0


There are many ways you can do this:

Form 1

String valueDouble= Double.toString(media1);
txtMedia1.setText(valueDouble);

Form 2

txtMedia1.setText(""+media1);

Form 3

txtMedia1.setText(String.valueOf(media1));

Form 4

Here would be basically equal to Form 3, where you can use the format to define the format you want, depending on how many boxes you show after the comma. See:

txtMedia1.setText(String.format("%.2f", value));
  • Ack Lay, I used all these forms, but when I click on the button to calculate the average application to work, some idea of what?

  • @Jeffersonalencar is easy to solve, but without you put the error is hard to know.

  • @Jeffersonalencar you put gettext in your Textview. You had to put findViewById; so the error happens.

  • `FATAL EXCEPTION: main Process: com.app.jeffersonalencar.composicao, PID: 3643 java.lang.Nullpointerexception: Attempt to invoke virtual method 'void android.widget.Textview.setText(java.lang.Charsequence)' on a null Object Reference at com.app.jeffersonalencar.composicao.act_mains.onClick(act_mains.java:49)
 at android.view.View.performClick(View.java:5637)
 at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751)``

  • at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:154)
 at android.app.ActivityThread.main(ActivityThread.java:6119)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

  • Follow above for error. @Acklay

  • @Jeffersonalencar the error is because of what I commented there above, just change that solve.

  • I’m currently trying to use this method that you gave me, I’ve tried others, but it always happens the same: txtMedia1.setText(String.valueOf(media1));

  • Does so: txtMedia1 = (Textview) findViewById(R.id.media2) ; txtMedia1.setText(String.valueOf(media1));

  • Puts Bicipalmedia in place of media1.

  • Error:(49, 49) error: cannot find Symbol variable media2

  • You have to see your Textview id in your XML.

  • The Textview id is txtMedia1. If so: txtMedia1.setText(String.valueOf(Bicipalmedia); txtMedia1 = (Textview) findViewById(R.id.txtMedia1) ;

  • First you have to instantiate Textview with Id. Thus: txtMedia1 = (Textview) findViewById(R.id.txtMedia1); then you define a value for it. txtMedia1.setText(""+Bicipalmedia)

  • Now yes! Thank you very much!

Show 10 more comments

Browser other questions tagged

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