Criei um app que contém somente uma activity. Nessa activity existem 3 EditTexts e 1 TextView.
Criei 3 variáveis do tipo long (l01, l02 e l03
) para receberem o valor dos 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));
}
}
Como podem ver no código utilizei o método parseLong, porém o mesmo não fez o app funcionar e o aplicativo nem abre.
No android monitor dá isso:
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)
Tentei mudar as seguinte linhas com o ".trim" como vi em Invalid long, porém o erro persistiu:
l01=Long.parseLong(et01.getText().toString().trim());
l02=Long.parseLong(et02.getText().toString().trim());
l03=Long.parseLong(et03.getText().toString().trim());