Tuesday, April 16, 2013

[android help] ClassCastException error, dont know why it occurs

java - ClassCastException error, dont know why it occurs - Stack Overflow







Tell me more ×

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

















I m working with BaseAdapter class and i get an ClassCastException error in getView method when i try to cast my viewHolder class.


Here is my code:



public View getView(int position, View convertView, ViewGroup parent){
// TODO Auto-generated method stub
NoteItem noteItem = list.get(position);
TextView tv;
LinearLayout ll;
if(convertView == null){
convertView = inflater.inflate(R.layout.dialog_listitem_note, null);
ll = (LinearLayout) convertView.findViewById(R.id.llNote);
tv = (TextView) convertView.findViewById(R.id.tvNoteItem);
convertView.setTag(new NoteItemViewHolder(tv, ll));
ll.setOnClickListener(new OnClickListener(){...});
}else{
NoteItemViewHolder viewHolder = (NoteItemViewHolder) convertView.getTag(); //ClassCastException error here
ll = viewHolder.getLl();
tv = viewHolder.getName();
}
tv.setText(noteItem.getName());
ll.setTag(noteItem);
...
return convertView;
}

private class NoteItemViewHolder{

TextView name;
LinearLayout ll;

public NoteItemViewHolder(TextView name, LinearLayout ll){
this.name = name;
this.ll = ll;
}

public TextView getName(){
return name;
}

public LinearLayout getLl(){
return ll;
}
}


Dont get why this error occurs, please help.


























getView method should Like below and Also Check Your Control Like LinearLayout is bind with R.id.yourLinearID



public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(view == null)
{
view = inflater.inflate(R.layout.checkin_item, null);
holder = new ViewHolder();

holder.iconImage = (ImageView)view.findViewById(R.id.chkin_item_iv);
holder.placeName = (TextView)view.findViewById(R.id.chkin_item_tv_name);
holder.placeAddress = (TextView)view.findViewById(R.id.chkin_item_tv_address);
holder.placeKm = (TextView)view.findViewById(R.id.chkin_item_tv_0);

view.setTag(holder);
}
else
{
holder = (ViewHolder)view.getTag();
}

try

if(googleSetGetArray.getGoogleSetGets().get(position).getName().length() != 0)
{
holder.placeName.setText(googleSetGetArray.getGoogleSetGets().get(position).getName());
}
else
{
holder.placeName.setText(mContext.getString(R.string.mfitem_lbl_namenotpresent));
}
if(googleSetGetArray.getGoogleSetGets().get(position).getVicinity() != null && googleSetGetArray.getGoogleSetGets().get(position).getVicinity().length() != 0)
{
holder.placeAddress.setText(googleSetGetArray.getGoogleSetGets().get(position).getVicinity());
}
else
{
holder.placeAddress.setText(mContext.getString(R.string.mfitem_lbl_addressnotpresent));
}
if(googleSetGetArray.getGoogleSetGets().get(position).getDistance() != 0.0f)
{
holder.placeKm.setText(Float.toString(googleSetGetArray.getGoogleSetGets().get(position).getDistance()));
}
else
{
holder.placeKm.setText(Float.toString(0.0f));
}
}catch(Exception ex)
{
ex.printStackTrace();
}


imageLoader.DisplayImage(data[position], holder.iconImage);
return view;
}

class ViewHolder
{
ImageView iconImage;
TextView placeName;
TextView placeAddress;
TextView placeKm;

}






















lang-java






.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...