我在调试应用程序时遇到ClassCastException。
obj = (CommonFeedBean) ResponseHandler_Review.vectorLocation1.elementAt(i);
我在上面的行中收到了ClassCastException。完整代码如下:
for(int i=0;i<ResponseHandler_Review.vectorLocation1.size();i++)
{
System.out.println("inside the for loop");
obj = (CommonFeedBean) ResponseHandler_Review.vectorLocation1.elementAt(i);
System.out.println("inside the obj++++++");
hr1 = new HorizontalFieldManager(){
protected void onFocus(int direction) {
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
invalidate();
super.onUnfocus();
}
public void paint(Graphics graphics) {
if (isFocus()) {
graphics.setBackgroundColor(Color.WHITE);
graphics.clear();
}
super.paint(graphics);
}
};
String arg[] ;
System.out.println("vectorlocation size " + ResponseHandler_Review.vectorLocation1.size());
arg = new String[ResponseHandler_Review.vectorLocation1.size()];
System.out.println("after arg++++++++++");
bit = new Bitmap[ResponseHandler_Review.vectorLocation1.size()];
scaleBit = new Bitmap[ResponseHandler_Review.vectorLocation1.size()];
System.out.println("after bit array+++++++");
for(int j = 0; j< ResponseHandler_Review.vectorLocation1.size(); j++){
arg[j] = String.valueOf(ResponseHandler_Review.vectorLocation1.elementAt(j));
System.out.println("Vector element is "+j+" = "+arg[j]);
bit[j] = response.getImage(arg[j]);
scaleBit[j] = new Bitmap(162, 120);
bit[j].scaleInto(scaleBit[j], Bitmap.FILTER_LANCZOS);
hr1.add(new BitmapField(scaleBit[j]));
}
try{
label[i] = new LabelField(obj.getTitle(), Field.FIELD_VCENTER|Field.FOCUSABLE)
{
public int getPreferredWidth()
{
return Display.getWidth()-150;
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
};
答案 0 :(得分:1)
确保ResponseHandler_Review.vectorLocation1.elementAt(i)
或不返回CommonFeedBean
。为避免ClassCastException
,请执行此操作。
for(int i=0;i<ResponseHandler_Review.vectorLocation1.size();i++) {
System.out.println("inside the for loop");
if(ResponseHandler_Review.vectorLocation1.elementAt(i) instanceof CommonFeedBean) {
// your code
} else {
// handle here.
}
答案 1 :(得分:0)
看来你vectorLocation1
中的任何内容都不是CommonFeedBean
。我会检查您填充该对象的位置,并确保您实际上将CommonFeedBean
放入其中。
如果你想知道obj
实际是什么,你可以做
System.out.println(obj.getClass().getName());