gpt4 book ai didi

java - Android/Java 变量可能超出范围

转载 作者:行者123 更新时间:2023-12-02 00:26:00 26 4
gpt4 key购买 nike

当我运行下面的代码时,一切都很顺利,直到我更改了eekBar的值。 onProgressChange 更改 myValue 的文本值。我强行关闭,它给了我下面的日志猫条目。

在进行调试时,抛出的异常表示找不到 ID 2,您可以看出它是 myValue,这是预料之中的。请让我知道你在想什么。谢谢

04-06 18:15:38.576: E/AndroidRuntime(25164):android.content.res.Resources$NotFoundException: String resource ID #0x2f
04-06 18:15:38.576: E/AndroidRuntime(25164): at android.content.res.Resources.getText(Resources.java:201)
04-06 18:15:38.576: E/AndroidRuntime(25164): at android.widget.TextView.setText(TextView.java:2877)
04-06 18:15:38.576: E/AndroidRuntime(25164): at thisPackage.Tabs.CustomSeekBar.onProgressChanged(CustomSeekBar.java:74)

ColorsActivity(主要)

public class ColorsActivity extends Activity
{
CustomSeekBar red = new CustomSeekBar();
RelativeLayout theItem;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.seekbarlist);
RelativeLayout thisOne = (RelativeLayout)findViewById(R.id.mainLayout);



attachLayout(thisOne, red, "red", 1);
//attachLayout(thisOne, "blue", 2);
//attachLayout(thisOne, "green", 3);

// Amarino.connect(this, "00:11:11:21:05:53");
}
public void attachLayout(RelativeLayout layout, CustomSeekBar object, String label, int id)
{

theItem = object.createCustomSeekBar(this, label);
theItem.setId(id);
RelativeLayout.LayoutParams myLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
myLayoutParams.addRule(RelativeLayout.BELOW,id-1);
theItem.setLayoutParams(myLayoutParams);
layout.addView(theItem, id);
}


}

自定义搜索栏

public class CustomSeekBar implements SeekBar.OnSeekBarChangeListener {

Context myContext;
RelativeLayout myLayout;
TextView myValue;
TextView myLabel;
SeekBar mySeekBar;

public void CustomSeekBar(){

}


public RelativeLayout createCustomSeekBar(Context context, String label){


myContext = context;
myLayout = new RelativeLayout(myContext);


mySeekBar = new SeekBar(myContext);
myLabel = new TextView(myContext);
myValue = new TextView(myContext);

mySeekBar.setId(1);
myLabel.setId(2);
myValue.setId(3);

RelativeLayout.LayoutParams mySeekBarParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams myLabelParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams myValueParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

int seekBarId = mySeekBar.getId();
int labelId = myLabel.getId();

myLabelParams.addRule(RelativeLayout.BELOW,seekBarId);
myValueParams.addRule(RelativeLayout.BELOW,seekBarId);
myValueParams.addRule(RelativeLayout.RIGHT_OF,labelId);

mySeekBar.setLayoutParams(mySeekBarParams);
myLabel.setLayoutParams(myLabelParams);
myValue.setLayoutParams(myValueParams);

myLabel.setText(label);
myValue.setText("hello");
mySeekBar.setOnSeekBarChangeListener(this);

myLayout.addView(myLabel);
myLayout.addView(myValue);
myLayout.addView(mySeekBar);


return myLayout;

}

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
myValue.setText(progress);
//Amarino.sendDataToArduino(myContext, "00:11:11:21:05:53", 'A', progress);
}
public void onStopTrackingTouch(SeekBar seekBar){

}
public void onStartTrackingTouch (SeekBar seekBar){
}


}

最佳答案

您将文本设置为 int,因此 Android 认为您正在尝试通过 id 引用资源。将 progress 转换为字符串应该可以解决您的问题。例如。 Integer.toString(进度)

关于java - Android/Java 变量可能超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10050318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com