Android Accessing a button's parent view
<p>I have a list of inflated rows that have two TextViews and two buttons. The buttons should increment/decrement one of the textViews. However when I try to implement this, I cannot get the correct tag to come back from within the onclickListener. It gives a null pointer exception. I have a holder and convertview implemented and the Holder does set the tag correctly for the convertview. More specifically I need to be able to get the tag for the convertview to change the row's value.</p>

<p>I put stars next to what I believe to be the important parts.</p>

<p>Here is the adapter code</p>

<pre><code>class PlayerAdapter extends ArrayAdapter&lt;Player&gt; {

    private ArrayList&lt;Player&gt; players;
    private Context cont;
    private Player temp;



    public Player getTemp() {
        return temp;
    }

    public void setTemp(Player temp) {
        this.temp = temp;
    }

    public Player getItem(int index){
        return players.get(index);
    }

    public PlayerAdapter(Context context, int layoutId, int textViewResourceId, ArrayList&lt;Player&gt; items) {
            super(context,layoutId, textViewResourceId, items);
            players = items;
            cont = context;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        ViewHolder holder;


        if(convertView == null){
            LayoutInflater inflater =getLayoutInflater();
            convertView = inflater.inflate(R.layout.player_slot,null);

             holder = new ViewHolder(convertView, position);
             holder.player_name = (TextView) convertView.findViewById(R.id.name);
             holder.player_score = (TextView) convertView.findViewById(R.id.score);
             holder.dec_score = (Button) convertView.findViewById(R.id.decrement);
             holder.inc_score = (Button) convertView.findViewById(R.id.increment);
             convertView.setTag(holder);


        }
        else holder = (ViewHolder) convertView.getTag();



        Button dec = (Button)convertView.findViewById(R.id.decrement);
        Button inc =(Button)convertView.findViewById(R.id.increment);
        TextView name = (TextView)convertView.findViewById(R.id.name);
        TextView score = (TextView)convertView.findViewById(R.id.score);

        System.out.println("tag = " +dec.getTag());

        Player temp = getPlayer(position);
        System.out.println(temp.getPlayer_name() + temp.getPlayer_score());
        if(temp != null){
            //holder.player_name.setText(temp.getPlayer_name());
            //holder.player_score.setText(""+temp.getPlayer_score());
            name.setText(temp.getPlayer_name());
            score.setText(""+temp.getPlayer_score());
        }

        dec.setOnClickListener(new OnClickListener(){
                private Button dec;

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            int position =  (Integer) ((View)v.getParent()).getTag();//***
            Player temp = getPlayer(position);
            System.out.println("Player "+ temp.getPlayer_name() + "'s score changed");
            temp.setPlayer_score(temp.getPlayer_score()-1);//**
            PlayerAdapter.this.notifyDataSetChanged();
            }

        });


        return convertView;
    }
</code></pre>

<p>}</p>
