How do I work with LONG "data" inserted in an editText?

Asked

Viewed 107 times

0

I tried to pass numbers that would be inserted in 3 editTexts for 3 variables long that I created: l01, l02, l03. To that way work them and make a very simple account.

Mainactivity.java:

package genesysgeneration.treerule;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class MainMenuActivity extends AppCompatActivity {

    private EditText et01, et02, et03;
    private TextView tv01;
    private long l01, l02, l03;

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

        l01=0;
        l02=0;
        l03=0;

        et01=(EditText)findViewById(R.id.et01);
        et02=(EditText)findViewById(R.id.et02);
        et03=(EditText)findViewById(R.id.et03);

        l01=et01;
        l02=et02;
        l03=et03;

        tv01=(TextView)findViewById(R.id.tv01);
        tv01.setText(l01*l02*l03);

    }
}

I also changed the 3 inputType from the 3 editTexts to number|numberDecimal:

inserir a descrição da imagem aqui

What did I do wrong, or what did I fail to do? The program did not even run errors in the following lines:

l01=et01;
l02=et02;
l03=et03;

tv01.setText(l01*l02*l03);

1 answer

1


First, you must use the method gettext to take the values of the fields Edittext and make the conversion to Long:

l01 = Long.parseLong(et01.getText().toString());
l02 = Long.parseLong(et02.getText().toString());
l03 = Long.parseLong(et03.getText().toString());

The method setText accepts a parameter of type Charsequence. You can make a conversion to String:

tv01.setText(String.valueOf(l01 * l02 * l03));
  • The last line error is gone, thank you! However it still gives error in the lines where I put for the variables (L01, L02, L03) receive the values of editTexts.

  • Sorry, I hadn’t noticed that there was more error in the code. I edited the answer with the rest of the code.

  • It went bad here. Look what happened: "close() was Never explicitly called on database '/data/data/com.google.android.gms/Databases/networkstatistics.sqlite' android.database.sqlite.Databaseobjectnotclosedexception: Application did not close the cursor or database object that was opened here" e mais "java.lang.RuntimeException: Unable to start activity ComponentInfo{genesysgeneration.treerule/genesysgeneration.treerule.MainMenuActivity}: java.lang.NumberFormatException: Invalid long: """

Browser other questions tagged

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