-1
listContent.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            for(int i=0;i<showLists.size();i++){
              //
                TextView v=(TextView)listContent.getChildAt(i).findViewById(R.id.txtDes);
                v.setTextColor(Color.BLACK);

            }

            TextView v=(TextView)listContent.getChildAt(position).findViewById(R.id.txtDes);
            v.setTextColor(Color.RED);
            Toast.makeText(context,"POS "+showLists.get(position).getDes(),Toast.LENGTH_SHORT).show();

        }
    });

I have been the problem with get position item of listview. Android just has shown about 12 row on devide's creen, when I click another item on listview (my listview have 30 item ) which shown the error. And this is error: "Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference" . Thanks for read.

  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – 1615903 May 05 '17 at 10:10

3 Answers3

0

Your question is indeed about Null Pointer Exception, but is harder to identify why this is happening. The problem can be found here:

for(int i=0;i<showLists.size();i++){ <-- this line actually causes the crash
//
    the crash is in the next line, at the findViewById
    TextView v=(TextView)listContent.getChildAt(i).findViewById(R.id.txtDes); 
    v.setTextColor(Color.BLACK);
}

Your for has the wrong upper limit because of a mechanism known as recycling, and because of this mechanism your list view will never have the same number of rows as the amount of data that needs to be displayed (read about recycling to understand this). Given this fact, we know for sure that the number of views found in list view (listContent.getChildCount()) will be smaller than showLists.size() and thus making the call listContent.getChildAt(i) to return a NULL value when the index equals listContent.getChildCount() creating the crash.

Now you might be tempted to change showLists.size() with listContent.getChildCount() and see that the app doesn't crash anymore, but if you click a row, then other rows are coloured also as you scroll the list (the recycling is again the problem). To really fix the problem you should save the index of the selected row and call notifyDatasetChanged, so when getView is called in adapter you simply check the current position to be displayed with the selected position. In case of equality you change the color of text to red, otherwise to black. Below, you will find some parts of an example:

int currentPosition = -1;

// Just a basic adapter. The getView method is the key here
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1) {
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            if (position == currentPosition) {
                ((TextView) view).setTextColor(Color.RED);
            } else {
                ((TextView) view).setTextColor(Color.BLACK);
            }
            return  view;
        }
    };

// and here is the onItemClick
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            currentPosition = position;
            adapter.notifyDataSetChanged();
        }
    });
Community
  • 1
  • 1
Iulian Popescu
  • 2,595
  • 4
  • 23
  • 31
0
listContent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            for (int i = 0; i < showLists.size(); i++) {

                TextView v = (TextView) view.findViewById(R.id.txtDes);
                v.setTextColor(Color.BLACK);

            }

            TextView v = (TextView) view.findViewById(R.id.txtDes);
            v.setTextColor(Color.RED);
            Toast.makeText(context, "POS " + showLists.get(position).getDes(), Toast.LENGTH_SHORT).show();

        }
    });
Bhadresh
  • 291
  • 1
  • 9
0
public View getView(int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewRow=convertView;
    if(viewRow==null){
        viewRow=inflater.inflate(layout,parent,false);
        viewHolder viewHolder=new viewHolder();
        viewHolder.imgIcon    = (ImageView) viewRow.findViewById(R.id.imgIcon);
        viewHolder.txtDes     = (TextView) viewRow.findViewById(R.id.txtDes);

        viewRow.setTag(viewHolder);
    }

    viewHolder  holder= (viewHolder) viewRow.getTag();
    holder.imgIcon.setImageResource(listMoiNhat.get(position).getIcon());
    holder.txtDes.setText(listMoiNhat.get(position).getDes());
    if(position==currentpos){
        holder.txtDes.setTextColor(Color.RED);
    }
    else {
        holder.txtDes.setTextColor(Color.RED);

    }
    return viewRow;
}

//and here is create customListMoiNhat object final customListMoiNhat customListMoiNhat=new customListMoiNhat(context,R.layout.moinhat_customlistview,showLists,currentpos); customListMoiNhat.notifyDataSetChanged(); listContent.setAdapter(customListMoiNhat);

    listContent.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {



            currentpos=position;
            customListMoiNhat.notifyDataSetChanged();


        }
    });