首页 > 解决方案 > 请H,错误:无法执行android的方法:onClick

问题描述

当我在没有输入任何内容的情况下单击按钮时,此错误

 public void Confirmer(View view) {

    if(TextUtils.isEmpty(min.getText())){
        Toast.makeText(ObjectifActivity.this,"text!",Toast.LENGTH_LONG).show();
    }

    if(TextUtils.isEmpty(max.getText())){
        Toast.makeText(ObjectifActivity.this,"text!",Toast.LENGTH_LONG).show();
    }

    int min_cal = Integer.parseInt(min.getText().toString());//This error
    int max_cal = Integer.parseInt(max.getText().toString());//This error

    if(max_cal < min_cal){
        Toast.makeText(ObjectifActivity.this,"text!", Toast.LENGTH_LONG).show();
    }else{

        SharedPreferences objectif = getSharedPreferences("objectif", MODE_PRIVATE);
        SharedPreferences.Editor editor = objectif.edit();
        editor.putInt("min",min_cal);
        editor.putInt("max", max_cal);
        editor.commit();
        Toast.makeText(ObjectifActivity.this, "ok!", Toast.LENGTH_LONG).show();
        Intent  intent = new Intent(this,MainActivity.class);
        startActivity(intent);

    }
}




    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Confirmer"
    android:id="@+id/button"
    android:background="@color/colorPrimary"
    android:onClick="Confirmer"
    android:layout_below="@+id/linearLayout2"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="121dp" />

标签: javaandroid

解决方案


   if (!TextUtils.isEmpty(min.getText().trim())) {
        if (!TextUtils.isEmpty(max.getText().trim())) {
            int min_cal = Integer.parseInt(min.getText().toString());
            int max_cal = Integer.parseInt(max.getText().toString());
            if (max_cal < min_cal) {
                Toast.makeText(ObjectifActivity.this, "text!", Toast.LENGTH_LONG).show();
            } else {
                SharedPreferences objectif = getSharedPreferences("objectif", MODE_PRIVATE);
                SharedPreferences.Editor editor = objectif.edit();
                editor.putInt("min", min_cal);
                editor.putInt("max", max_cal);
                editor.commit();
                Toast.makeText(ObjectifActivity.this, "ok!", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
            }
        } else {
            Toast.makeText(ObjectifActivity.this, "max value is empty!", Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(ObjectifActivity.this, "min value is empty!", Toast.LENGTH_LONG).show();
    }
}

推荐阅读