I am getting a ClassCastException while debugging my application.
obj = (CommonFeedBean) ResponseHandler_Review.vectorLocation1.elementAt(i);
I am getting ClassCastException at the above line. Full code follows:
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) {
开发者_JAVA技巧 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;
}
};
Make sure the ResponseHandler_Review.vectorLocation1.elementAt(i)
returns the CommonFeedBean
or not. For avoiding ClassCastException
, do like this.
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.
}
It would seem that whatever is in your vectorLocation1
is not a CommonFeedBean
. I would check wherever you're populating that object and make sure you are actually putting CommonFeedBean
s in there.
If you want to know what obj
actually is, you can do
System.out.println(obj.getClass().getName());
精彩评论