I m getting error in getView() in android? - android

I have a listview and custom adapter....when I run program it will give me error in getview.How can I resolve that error.
code:-
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = View.inflate(m_Context, R.layout.deallisting_card_view, null);
viewHolder.m_Header = (TextView) convertView.findViewById(R.id.headingText);
viewHolder.m_Subheader = (TextView) convertView.findViewById(R.id.subHeaderText);
viewHolder.m_DummyText = (TextView) convertView.findViewById(R.id.subHeadingText);
viewHolder.m_logoImage = (ImageView) convertView.findViewById(R.id.appImage);
viewHolder.m_getBtn = (Button) convertView.findViewById(R.id.getDealBtn);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
}
in covertview = View.inflate(m_Context...); in this line I am getting error

this should be done like this.
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.deallisting_card_view, null);
viewHolder.m_Header = (TextView) convertView.findViewById(R.id.headingText);
viewHolder.m_Subheader = (TextView) convertView.findViewById(R.id.subHeaderText);
viewHolder.m_DummyText = (TextView) convertView.findViewById(R.id.subHeadingText);
viewHolder.m_logoImage = (ImageView) convertView.findViewById(R.id.appImage);
viewHolder.m_getBtn = (Button) convertView.findViewById(R.id.getDealBtn);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
}

public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder= new ViewHolder();
vi=convertView;
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
vi= mInflater.inflate( R.layout.deallisting_card_view, null);
viewHolder.m_Header = (TextView) convertView.findViewById(R.id.headingText);
viewHolder.m_Subheader = (TextView) convertView.findViewById(R.id.subHeaderText);
viewHolder.m_DummyText = (TextView) convertView.findViewById(R.id.subHeadingText);
viewHolder.m_logoImage = (ImageView) convertView.findViewById(R.id.appImage);
viewHolder.m_getBtn = (Button) convertView.findViewById(R.id.getDealBtn);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
}
Try above code, hope it will work :)GlbMP

Related

in listview android items repeating

public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService("layout_inflater");
if (convertView != null) {
return convertView;
}
list = new View(this.mContext);
list = inflater.inflate(R.layout.rowsset, null);
imageView = (ImageView) list.findViewById(R.id.pic1);
textView1 = (TextView) list.findViewById(R.id.det2);
textView2 = (TextView) list.findViewById(R.id.kite1);
textView1.setText(this.web[position]);
textView2.setText(this.web1[position]);
imageView.setImageResource(this.Imageid[position]);
return list;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.rowsset, parent, false);
imageView = (ImageView) convertView.findViewById(R.id.pic1);
textView1 = (TextView) convertView.findViewById(R.id.det2);
textView2 = (TextView) convertView.findViewById(R.id.kite1);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
viewHolder.textView1.setText(this.web[position]);
viewHolder.textView2.setText(this.web1[position]);
viewHolder.imageView.setImageResource(this.Imageid[position]);
// Return the completed view to render on screen
return convertView;
}
// Please this line mention in your adapter's constructor
inflater = LayoutInflater.from(context);
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.rowsset, null);
imageView = (ImageView) list.findViewById(R.id.pic1);
textView1 = (TextView) list.findViewById(R.id.det2);
textView2 = (TextView) list.findViewById(R.id.kite1);
textView1.setText(web[position]);
textView2.setText(web1[position]);
imageView.setImageResource(Imageid[position]);
return convertView;
}
Hope this will work...

Listview Repeating Items

I trying make a listview with much data but when i build, item data on listview multiple repeating.
This is myJava CustomAdapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.Name = (TextView) convertView.findViewById(R.id.Recipe_Name);
holder.Image_Block = (ImageView) convertView.findViewById(R.id.Recipe_Image);
holder.Text_Recipe = (TextView) convertView.findViewById(R.id.Recipe_Text);
holder.Text_Rarity = (TextView) convertView.findViewById(R.id.Recipe_Rarity);
RowItem row_pos = rowItems.get(position);
holder.Image_Block.setImageResource(row_pos.getImage_Block());
holder.Name.setText(row_pos.getName());
holder.Text_Recipe.setText(row_pos.getText_Recipe());
holder.Text_Rarity.setText(row_pos.getText_Rarity());
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
Anyone can fix ? Thanks
Your data isn't repeating--when you're recycling an old view, you're not initializing it to new values. Move the code that sets values (not the code that makes connections) outside of the if-else statement.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.Name = (TextView) convertView.findViewById(R.id.Recipe_Name);
holder.Image_Block = (ImageView) convertView.findViewById(R.id.Recipe_Image);
holder.Text_Recipe = (TextView) convertView.findViewById(R.id.Recipe_Text);
holder.Text_Rarity = (TextView) convertView.findViewById(R.id.Recipe_Rarity);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
RowItem row_pos = rowItems.get(position);
holder.Image_Block.setImageResource(row_pos.getImage_Block());
holder.Name.setText(row_pos.getName());
holder.Text_Recipe.setText(row_pos.getText_Recipe());
holder.Text_Rarity.setText(row_pos.getText_Rarity());
return convertView;
}

Custom listview throwing a null pointer exception

I am creating a custom ListView in android. The problem am facing is in my adapter code. It throws a NullPointerException even though everything else seems in order. Sample code below:
public class CustomAdapter extends BaseAdapter {
private Context ctx;
private List<LearnLangs> langsList;
private LayoutInflater layoutInflater;
public CustomAdapter(Context ctx, List<LearnLangs> langsList) {
this.ctx = ctx;
this.langsList = langsList;
}
#Override
public int getCount() {
return langsList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public ImageView img;
public TextView title;
public TextView sub;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LearnLangs langs = langsList.get(position);
if (convertView == null) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.custom_list, parent, false);
holder = new ViewHolder();
//error occurs on this line below
holder.img = (ImageView) convertView.findViewById(R.id.list_img);
holder.title = (TextView) convertView.findViewById(R.id.list_title);
holder.sub = (TextView) convertView.findViewById(R.id.list_subT);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
holder.img.setImageResource(langs.getImgId());
holder.title.setText(langs.getNameLang());
holder.sub.setText(langs.getnTuts());
}
return convertView;
}
}
Exception thrown:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.example.joey.sunshine.CustomAdapter.getView(CustomAdapter.java:62)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.ListView.makeAndAddView(ListView.java:1864)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillFromTop(ListView.java:759)
at android.widget.ListView.layoutChildren(ListView.java:1673)
at android.widget.AbsListView.onLayout(AbsListView.java:2151)
at android.view.View.layout(View.java:15761)
What seems to be the problem?
You are getting exception because you are not using view which you are inflating at all. Change your code of getview() method as per below :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View mView = convertView;
ViewHolder holder;
LearnLangs langs = langsList.get(position);
if (mView == null) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = layoutInflater.inflate(R.layout.custom_list, parent, false);
holder = new ViewHolder();
//error occurs on this line below
holder.img = (ImageView) mView.findViewById(R.id.list_img);
holder.title = (TextView) mView.findViewById(R.id.list_title);
holder.sub = (TextView) mView.findViewById(R.id.list_subT);
mView.setTag(holder);
}
else
{
holder = (ViewHolder) mView.getTag();
holder.img.setImageResource(langs.getImgId());
holder.title.setText(langs.getNameLang());
holder.sub.setText(langs.getnTuts());
}
return mView;
}
Just change this line :
layoutInflater.inflate(R.layout.custom_list, parent, false);
to this:
convertView = layoutInflater.inflate(R.layout.custom_list, parent, false);
EDIT
public AdapterConstructor() {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //move this line inside the constructor
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LearnLangs langs = langsList.get(position);
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.custom_list, parent, false);
holder = new ViewHolder();
//error occurs on this line below
holder.img = (ImageView) convertView.findViewById(R.id.list_img);
holder.title = (TextView) convertView.findViewById(R.id.list_title);
holder.sub = (TextView) convertView.findViewById(R.id.list_subT);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.img.setImageResource(langs.getImgId());
holder.title.setText(langs.getNameLang());
holder.sub.setText(langs.getnTuts());
return convertView;
}
Just change your getView() method by below :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LearnLangs langs = langsList.get(position);
if (convertView == null) {
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_list, parent, false);
holder = new ViewHolder();
//error occurs on this line below
holder.img = (ImageView) convertView.findViewById(R.id.list_img);
holder.title = (TextView) convertView.findViewById(R.id.list_title);
holder.sub = (TextView) convertView.findViewById(R.id.list_subT);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.img.setImageResource(langs.getImgId());
holder.title.setText(langs.getNameLang());
holder.sub.setText(langs.getnTuts());
return convertView;
}

ListView Changes Data on Scroll

I have a ListView that is correctly populated with an array on create, but when I scroll, the data changes and is all out of order. I don't know wether the problem is in my CustomListViewAdapter or in my DetailsPage. Here's the For Loop that I am using to generate the textViews:
if (currentObject.setTime != null && currentObject.setName != null) {
String[] temporaryNames = currentObject.setName;
int totalNames = (currentObject.setTime.length / currentObject.setName.length);
for (int i = 1; i < totalNames; i++) {
currentObject.setName = ArrayUtils.addAll(currentObject.setName,temporaryNames);
}
}
listView.setAdapter(new BusRouteCustomListViewAdapter(this, currentObject.setName, currentObject.setTime));
and here is the code for my customListViewAdapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View v = convertView;
if (v == null)
if (locations[position] != null && times[position] != null) {
v = inflater.inflate(R.layout.busdetailrow, null);
holder.tv1 = (TextView) v.findViewById(R.id.text);
holder.tv2 = (TextView) v.findViewById(R.id.text2);
holder.tv1.setText(locations[position]);
holder.tv2.setText(times[position]);
} else {
v = inflater.inflate(R.layout.null_row, null);
}
return v;
}
You replace the code with these lines of code
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Holder holder;
View v = convertView;
if (v == null)
{
holder = new Holder();
v = inflater.inflate(R.layout.busdetailrow, null);
holder.tv1 = (TextView) v.findViewById(R.id.text);
holder.tv2 = (TextView) v.findViewById(R.id.text2);
v.setTag(holder);
} else {
holder = (Holder) v.getTag();
}
holder.tv1.setText(locations[position]);
holder.tv2.setText(times[position]);
return v;
}
Use your getView() like below.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
if (convertView == null){
if (locations[position] != null && times[position] != null) {
convertView = inflater.inflate(R.layout.busdetailrow, null);
holder.tv1 = (TextView) v.findViewById(R.id.text);
holder.tv2 = (TextView) v.findViewById(R.id.text2);
holder.tv1.setText(locations[position]);
holder.tv2.setText(times[position]);
} else {
}
convertView.setTag(holder);
}else{
holder = (Holder) convertView.getTag();
}
return convertView;
}
You didnt implement design holder probably. make following changes to your getView then it will work fine
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
if (convertView == null){
if (locations[position] != null && times[position] != null) {
convertView = inflater.inflate(R.layout.busdetailrow, null);
holder.tv1 = (TextView) v.findViewById(R.id.text);
holder.tv2 = (TextView) v.findViewById(R.id.text2);
holder.tv1.setText(locations[position]);
holder.tv2.setText(times[position]);
}
else
{
}
convertView.setTag(holder);
}else{
holder = (Holder) convertView.getTag();
}
return convertView;
}

Android listview ImageButton onclick error

I use an imagebutton in a listview and everything was working fine. But when i tried to change the background resource on the imagebutton on its click i got my app crashed. here is my code.
public View getView(final int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
// mCheckStates = new SparseBooleanArray(name1.size());
mInflater = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.textView1);
holder.tv1 = (TextView) convertView
.findViewById(R.id.textView2);
holder.b = (ImageButton) convertView
.findViewById(R.id.btn_invite);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(name1.get(position));
holder.tv1.setText(phno1.get(position));
holder.b.setTag(position);
holder.b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
RelativeLayout rl = (RelativeLayout)v.getParent();
holder.b = (ImageButton)rl.getChildAt(0);
holder.b.setBackgroundResource(R.drawable.ic_invited);
});
return convertView;
}
This following would be right flow-
public View getView(final int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
// mCheckStates = new SparseBooleanArray(name1.size());
mInflater = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.textView1);
holder.tv1 = (TextView) convertView
.findViewById(R.id.textView2);
holder.b = (ImageButton) convertView
.findViewById(R.id.btn_invite);
holder.tv.setText(name1.get(position));
holder.tv1.setText(phno1.get(position));
holder.b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
RelativeLayout rl = (RelativeLayout)v.getParent();
holder.b = (ImageButton)rl.getChildAt(0); // if your error is layout can't be casted to image Button, then in this line may be you are getting an unexpected layout view instead of a ImageButton
holder.b.setBackgroundResource(R.drawable.ic_invited);
});
holder.b.setTag(position);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}

Categories

Resources