我正在使用Seekbar库,所以当我拖动探索者时,我希望文本视图能够与探索者的值更新,但不幸的是,我的应用程序崩溃了。我收到一个错误,上面写着“在TextView上找不到资源”。守则如下:
RangeSeekBar seekBar1;
seekBar1 = (RangeSeekBar)rootView.findViewById(R.id.seekBar);
seekBar1.setValue(10);
seekBar1.setOnRangeChangedListener(new RangeSeekBar.OnRangeChangedListener() {
@Override
public void onRangeChanged(RangeSeekBar view, float min, float max, boolean isFromUser) {
seekBar1.setProgressDescription((int)min+"%");
TextView txtAmount;
txtAmount = (TextView)rootView.findViewById(R.id.txtAmount);
txtAmount.setText((int) min);
}
});发布于 2017-08-28 20:59:26
解决方案:--您不能像这样将int设置为TextView,请尝试如下:
txtAmount.setText(Float.toString(min));您使用的重载将查找字符串资源标识符,在本例中不存在该标识符。下面是以更正一为参数的CharSequence (字符串是一个CharSequence)。
很高兴知道:--如果您现在想知道int如何成为setText的参数,这是相当简单的。在您的应用程序中,您可以有一个strings.xml文件,该文件定义了应用程序中使用的一组资源字符串:
<resources>
<string name="test">This is a test</string>
</resources>有了这个定义,您可以在您的TextView上以如下方式显示文本:
txtAmount.setText(R.string.test);发布于 2017-08-28 21:00:14
如果您将一个整数传递给setText,则android期望该值是一个资源。系统正在试图找到一个id等于min的资源。您需要将min转换为字符串。
因此,要使其工作,请将txtAmount.setText((int) min);更改为txtAmount.setText(String.valueOf(min));
https://stackoverflow.com/questions/45927247
复制相似问题