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.
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());