如何解决在Android中的EditText中设置文本时出错
我在Android中有一个editText元素,当我想要设置文本(按下按钮后)时,出现以下错误:
android.content.res.Resources $ NotFoundException:字符串资源ID #0x2
这是我的Java代码中有问题的部分:
if(view.getId()==R.id.addBtn) { // A buttom is pressed
int quantity= Integer.parseInt(binding.itemEditText.getText().toString()); //Read the current value of the EditText Element
if(quantity<100) {
quantity++; //Increase the value by 1
binding.itemEditText.setText(quantity); //Set the value of the EditText element
}
}
这是编辑文本的XML代码
<EditText
android:id="@+id/itemEditText"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:inputType="number"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"
android:minEms="1"
android:textAlignment="center"
android:textAllCaps="false"
android:background="@android:color/transparent"
android:fontFamily="sans-serif-medium"
/>
有人知道为什么会出现此问题吗?我会很感激每个评论。
解决方法
替换
binding.itemEditText.setText(quantity);
到
binding.itemEditText.setText(quantity+"");
因为setText方法具有两个变体:具有String参数和int参数。最后一个变种是字符串资源ID
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。