viv
viv

Reputation: 6177

Android listView: NullPointerException

I need a small help in List view. I am generating a list View, but whenever i do getChildAt(int position) it throws NullPointerException.

Here is the code

_list=(ListView)findViewById(android.R.id.list);

_loadListElements();

_showListUI();

_list.getChildAt(1).setBackgroundColor(Color.WHITE);

Upvotes: 0

Views: 3022

Answers (4)

Anju Dahiya
Anju Dahiya

Reputation: 11

I tried by ever mean in OnListItemClickListener(),but fails. At last I made some modification in my customized adapter for listview. Here in getView(),I apply clickListener on the item which i added into the list frequently. n do all required functionality there. Here is my Code, where i add Image View in the list n so apply listener on imageview.

getChildAt(i) throws exception because it works only on visible items-1. So it doesn't work.

I Think it will help those who want to change the color when specific List Item is >selected. Go For it..

In getView() of customized Adapter //---------------------------------code------------------------------------------

LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View rowView = inflater.inflate(R.layout.icon_image_layout, parent, false); ImageView imageView = (ImageView) rowView.findViewById(R.id.Icon_ImageView); imageView.setClickable(true); final int pos=position; imageView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub // TODO Auto-generated method stub try{ if(previous_view!=null) previous_view.setBackgroundColor(Color.WHITE); }catch (Exception e) { System.out.println("Exception Occurs Previous View"); } v.setBackgroundColor(Color.RED); MainActivity.imageView.setImageResource(MainActivity.Image_Name[pos]); previous_view=v; return false; } });

Upvotes: 1

viv
viv

Reputation: 6177

Got it working............. In the getView(), the reason why it was not working earlier according to me was that,

For a condition check... for ex : if(condition matched) change the color;

This was causing problem when list used to update itself......

So correct would be : if(condition) [do something]; else [restore to original]

It was because that i did not had else statement i was getting problem.

Upvotes: 0

John
John

Reputation: 810

Without knowing quite what functionality you are trying to achieve, I'm not sure if either of these suggestions will be relevant.

Instead of using list.getChildAt(), can you set the background color from within the getView() method of whatever Adapter you are using? You'll have the view, but I don't know if you'll have the data yet to know which one to change the background on.

From one of your comments, it sounds like you are wanting to get the view of whatever list item was just clicked. The view "v" provided in ListActivity.onListItemClick() seems like it should be the view that you are wanting to work with.

Hope one of those two helps.

Upvotes: 1

Octavian Helm
Octavian Helm

Reputation: 39605

If you don't have more than one child element then one will certainly give you a NullPointerException as you have to start counting from null upwards in prgramming. So you might want to try this.

_list.getChildAt(0).setBackgroundColor(Color.WHITE);

But without more code and a logcat extract of the error stack it is hard to tell.

Upvotes: 1

Related Questions