Error using Long.parseLong to capture value from an EditText to a variable of type LONG

0

I've created an app that contains only one activity. In this activity there are 3 EditTexts and 1 TextView .

I created 3 variables of type long ( l01, l02 e l03 ) to receive the value of 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 parseLong method, but it did not make the app work and the application does not even open.

Ontheandroidmonitorgivesthis:

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 changing the following lines with the ".trim" as seen 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());
    
asked by anonymous 30.01.2017 / 21:57

1 answer

0

If you choose not to use a button to retrieve values only after clicking, you can use addTextChangeListener() . So, on the media you add the value in EditText, it will already assign to long . See:

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 as soon as you click 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));
    }
});
    
30.01.2017 / 22:16