Android edittext中的数字错误,然后对其进行数学运算并显示结果

Android edittext中的数字错误,然后对其进行数学运算并显示结果,android,android-edittext,Android,Android Edittext,下面是代码,但当我启动应用程序时,它会崩溃 public class Main2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); ImageButton imageBu

下面是代码,但当我启动应用程序时,它会崩溃

public class Main2Activity extends AppCompatActivity  {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton);
    ImageButton imageButton2 = (ImageButton) findViewById(R.id.imageButton2);
    ImageButton imageButton3= (ImageButton) findViewById(R.id.imageButton3);
    ImageButton imageButton4 = (ImageButton) findViewById(R.id.imageButton4);
    ImageButton imageButton5 = (ImageButton) findViewById(R.id.imageButton5);
    ImageButton imageButton6 = (ImageButton) findViewById(R.id.imageButton6);
    ImageButton imageButton7 = (ImageButton) findViewById(R.id.imageButton7);
    ImageButton imageButton8 = (ImageButton) findViewById(R.id.imageButton8);
    ImageButton imageButton9 = (ImageButton) findViewById(R.id.imageButton9);
    final EditText editText = (EditText) findViewById(R.id.editText);
    EditText editText2 = (EditText) findViewById(R.id.editText2);
    EditText editText3 = (EditText) findViewById(R.id.editText3);
    final TextView textView50 = (TextView)findViewById(R.id.textView50);
    //here where the android monitor says it error

    final int dayNumber = Integer.parseInt(editText.getText().toString());
    editText2.getText();
    editText3.getText();
    imageButton3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        int a = dayNumber * 4;
            textView50.setText(String.valueOf(a));
        }
    });
这是android监视器的结果

E/AndroidRuntime: FATAL EXCEPTION: main
Process: ahmednageeb.com.yourageinotherplanets, PID: 19858
java.lang.RuntimeException: Unable to start activity ComponentInfo{ahmednageeb.com.package/ahmednageeb.com.package.Main2Activity}: java.lang.NumberFormatException: Invalid int: ""
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
   at android.app.ActivityThread.-wrap11(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5417)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NumberFormatException: Invalid int: ""
   at java.lang.Integer.invalidInt(Integer.java:138)
   at java.lang.Integer.parseInt(Integer.java:358)
   at java.lang.Integer.parseInt(Integer.java:334)
   at ahmednageeb.com.package.Main2Activity.onCreate(Main2Activity.java:40)
   at android.app.Activity.performCreate(Activity.java:6237)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:5417) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
07-31 19:45:56.587 19858-19858/ahmednageeb.com.package I/Process: Sending signal. PID: 19858 SIG: 9

在实际单击按钮时解析EditText,而不是在加载视图时

imageButton3.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        int dayNumber = 0;
        String s =  editText.getText().toString();
        if (!s.isEmpty()) {
            dayNumber = Integer.parseInt(s);
        } 

        // Unused variables, but actually get the value if needed
        String s2 = editText2.getText().toString();
        String s3 = editText3.getText().toString();

        int a = dayNumber * 4;
        textView50.setText(String.valueOf(a));
    }
});

您正在尝试将空字符串解析为整数。不要这样做that@circket_007非常感谢您的回答,我是java新手,您可以解释更多,这是上面的评论,不是答案。请看下面我的实际答案,如果它解决了问题,请使用旁边的复选标记。非常感谢两种解决方案都有效,但现在stackoverflow不让我选择两种答案,我该怎么办?你们都对我很有帮助