Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextSwitcher NullPointer errors

I am really stuck on this. I am trying to do a simple textswitcher which will increment a quantity and update a price based on the quantity. Right now in my xml i have something like a TextView within a TextSwitcher just to increment the quantity. I get the textview with findViewById(R.id.quantity).

so this is what i have to find to setup the increment quantity ( I am implemementing ViewFactory)

switcher = (TextSwitcher) findViewById(R.id.switcher);
switcher.setFactory(this);
quantity = (TextView) findViewById(R.id.quantity);

I am also overriding the makeView()

@Override
     public View makeView() {
        return quantity;
    }

Also when an increment button is pressed i increment the counter and set the text on the switcher to the current count. Like this:

switcher.setText(String.valueOf(currentQuantity));

Can someone let me know what I am doing wrong?? I keep getting my nullpointer at this line:

switcher.setFactory(this);

Here is the XML snippet:

<TextSwitcher android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/switcher">
            <TextView android:text="TextView" android:id="@+id/quantity" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
        </TextSwitcher>
like image 406
HAxxor Avatar asked Jun 14 '11 19:06

HAxxor


1 Answers

From the Documentation for TextSwitcher:

setText (CharSequence text) Sets the text of the next view and switches to the next view. This can be used to animate the old text out and animate the next text in.

Which means you will need at least two text views, one with the old text, and one to receive the new text and animate in. The following XML should work:

    <TextSwitcher 
        android:id="@+id/counter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </TextSwitcher>
like image 181
Jave Avatar answered Oct 22 '22 04:10

Jave