Error using Long.parseLong to capture Edittext value for a LONG variable

Asked

Viewed 143 times

0

I created an app that contains only one Activity. In this app there are 3 Edittexts and 1 Textview.

I created 3 variables of the type long (l01, l02 e l03) to receive the value of the 3 Edittexts.

Mainactivity:

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=Long.parseLong(et01.getText().toString());
        l02=Long.parseLong(et02.getText().toString());
        l03=Long.parseLong(et03.getText().toString());

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

    }
}

As you can see in the code I used the method parseLong, but the same didn’t make the app work and the app doesn’t even open.

inserir a descrição da imagem aqui

On the android monitor gives it:

01-30 20:41:31.441 2448-2448/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{genesysgeneration.treerule/genesysgeneration.treerule.MainMenuActivity}: java.lang.NumberFormatException: Invalid long: ""
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
                                                     at android.app.ActivityThread.access$600(ActivityThread.java:123)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
                                                     at android.os.Handler.dispatchMessage(Handler.java:99)
                                                     at android.os.Looper.loop(Looper.java:137)
                                                     at android.app.ActivityThread.main(ActivityThread.java:4424)
                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                     at java.lang.reflect.Method.invoke(Method.java:511)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
                                                     at dalvik.system.NativeStart.main(Native Method)
                                                  Caused by: java.lang.NumberFormatException: Invalid long: ""
                                                     at java.lang.Long.invalidLong(Long.java:125)
                                                     at java.lang.Long.parseLong(Long.java:346)
                                                     at java.lang.Long.parseLong(Long.java:319)
                                                     at genesysgeneration.treerule.MainMenuActivity.onCreate(MainMenuActivity.java:27)
                                                     at android.app.Activity.performCreate(Activity.java:4466)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
                                                     at android.app.ActivityThread.access$600(ActivityThread.java:123) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                     at android.os.Looper.loop(Looper.java:137) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:4424) 
                                                     at java.lang.reflect.Method.invokeNative(Native Method) 
                                                     at java.lang.reflect.Method.invoke(Method.java:511) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
                                                     at dalvik.system.NativeStart.main(Native Method) 

I tried to change the following lines with ". Trim" as I saw in Invalid long, but the error persisted:

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

1 answer

0


If you choose not to use a button to redeem the values only after clicking, you can use the addTextChangeListener(). This way, in the media that adds the value in Edittext, it will already assign to long. Behold:

Edittext 1

et01.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

          l01=Long.parseLong(et01.getText().toString().trim());
          tv01.setText(String.valueOf(l01*l02*l03));

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

Edittext 2

et02.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

          l02=Long.parseLong(et02.getText().toString().trim());
          tv01.setText(String.valueOf(l01*l02*l03));

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

Edittext 3

et03.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

          l03=Long.parseLong(et03.getText().toString().trim());
          tv01.setText(String.valueOf(l01*l02*l03));

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

However you can add a button and redeem the values the moment you click on it. This way:

meuBotao.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

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

        tv01.setText(String.valueOf(l01*l02*l03));
    }
});

Browser other questions tagged

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