Prem
Prem

Reputation: 83

onClickListener on a button that is added through a layout inflator

Here is my code so far based on various answers to related questions on SO.

In my Activity

    GridView gv = (GridView) findViewById(R.id.gridView1);
    MyCustomAdapter mAdapter = new MyCustomAdapter();
    mAdapter.addItem("Action1");
    mAdapter.addItem("Action2");
    mAdapter.addItem("Action3");
    gv.setAdapter(mAdapter);

My Adapter

private class MyCustomAdapter extends BaseAdapter {

    private ArrayList<String> mData = new ArrayList<String>();
    private LayoutInflater mInflater;

    public MyCustomAdapter() {
        mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(final String item) {
        mData.add(item);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public String getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        NumericViewHolder holder = new NumericViewHolder();
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.horizontalnumberpicker, null);

            holder.textView = (TextView)convertView.findViewById(R.id.txtNPTitle);
            holder.minus = (Button)convertView.findViewById(R.id.btnMinus);                
            holder.plus = (Button)convertView.findViewById(R.id.btnPlus);                
            holder.value = (TextView)convertView.findViewById(R.id.txtNPValue);

            holder.minus.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    TextView tv = (TextView) v.findViewById(R.id.txtNPValue);
                    int value = Integer.parseInt(tv.getText().toString());
                    Toast.makeText(getApplicationContext(), Integer.toString(value), Toast.LENGTH_SHORT).show();
                    if (value > 0) {
                        value = value - 1;
                        tv.setText(Integer.toString(value));
                    }
                }
            });

            holder.plus.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    TextView tv = (TextView) v.findViewById(R.id.txtNPValue);
                    int value = Integer.parseInt(tv.getText().toString());    <--Error is here
                    Toast.makeText(getApplicationContext(), Integer.toString(value), Toast.LENGTH_SHORT).show();
                    value = value + 1;
                    tv.setText(Integer.toString(value)); 
                }
            });
            convertView.setTag(holder);
        } else {
            holder = (NumericViewHolder)convertView.getTag();
        }

        holder.textView.setText(mData.get(position));
        return convertView;
    }
}

XML - Main_Activity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rlAddProduct"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/btnSaveProduct"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginEnd="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp"
    android:text="@string/Save" />

<TextView
    android:id="@+id/lblSelectCategory"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/btnSaveProduct"
    android:layout_alignBottom="@+id/btnSaveProduct"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"
    android:text="@string/selectCategory" />

<Spinner
    android:id="@+id/spCategory"
    android:layout_width="250dp"
    android:layout_height="40dp"
    android:layout_alignBottom="@+id/lblSelectCategory"
    android:layout_marginLeft="33dp"
    android:layout_marginStart="33dp"
    android:layout_toEndOf="@+id/lblSelectCategory"
    android:layout_toRightOf="@+id/lblSelectCategory" />

<GridView
    android:id="@+id/gridView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/lblSelectCategory"
    android:layout_below="@+id/btnSaveProduct"
    android:layout_marginTop="14dp"
    android:numColumns="auto_fit" >
</GridView>

</RelativeLayout>

XML - Horizontalnumberpicker

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/txtNPTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="EnterTextHere" 
    android:layout_marginTop="5dp" />

<Button
    android:id="@+id/btnPlus"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignBaseline="@+id/txtNPValue"
    android:layout_alignBottom="@+id/txtNPValue"
    android:layout_toRightOf="@+id/txtNPValue"
    android:text="+" />

<TextView
    android:id="@+id/txtNPValue"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignTop="@+id/btnMinus"
    android:layout_toRightOf="@+id/btnMinus"
    android:gravity="center"
    android:text="0"
    android:textAppearance="@style/AppBaseTheme"
    android:textSize="20dp" />

<Button
    android:id="@+id/btnMinus"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/txtNPTitle"
    android:layout_marginTop="2dp"
    android:text="-" />

</RelativeLayout>

The requirement is that when I press the plus button, I need to increment the value in the corresponding txtNPValue. Similarly with the minus button, I need to decrement the value in the txtNPValue.

The error is

01-31 22:40:10.854: E/AndroidRuntime(32198): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference

Also I do not know if this is the right way to program such a requirement and would like some pointers.

Upvotes: 1

Views: 299

Answers (2)

JAAD
JAAD

Reputation: 12379

please remove:

 TextView tv = (TextView) v.findViewById(R.id.txtNPValue);

from onClick to resolve nullpointerException

Upvotes: 0

Nidhin Prathap
Nidhin Prathap

Reputation: 722

TextView tv = (TextView) v.findViewById(R.id.txtNPValue);

this will return null pointer exception, as the view that u r getting is of the button or the clicked item..

Use this :-

RelativeLayout rlLayout = (RelativeLayout) v.getParent();

TextView tv = (TextView) rlLayout.findViewById(R.id.txtNPValue);

Upvotes: 1

Related Questions