java - 请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" />
解决方案
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();
}
}
推荐阅读
- android - kotlin.UninitializedPropertyAccessException:lateinit 属性首选项尚未初始化
- apache - worker.properties 是强制在 Apache http 服务器中启用负载平衡和粘性会话的吗
- reactjs - React/Electron 不加载位于公共目录中的静态文件
- ruby-on-rails - docx 文件的回形针上传被下载为 zip 文件?
- java - 在 Aspose Words for Android 中获取段落中字符样式文本的起始位置
- python - 使用应用返回错误基于 2 列创建新列
- rabbitmq - Rabbitmq 服务器启动失败
- react-native - 在 react-native-nfc-manager v2 中禁用声音
- python - 有没有办法在输入后预先写一些东西?
- rust - Rust 为模块保留的名称