Jay
Jay

Reputation: 169

TextView not Found

This is driving me absolutely crazy and Im not sure what is going on. I have an xml layout with a TextView within a clickable RelativeLayout.

<RelativeLayout
            android:id="@+id/bg_section"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:background="@color/almost_black"
            android:clickable="true"
            android:onClick="goToBG"
            android:padding="10dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:text="Go To B.G."
                android:textColor="@color/white"
                android:textSize="20sp" />

            <ImageView
                android:id="@+id/bg_arrow"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="-10dp"
                android:src="@drawable/arrow_icon" />

            <TextView
                android:id="@+id/current_bg_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@id/bg_arrow"
                android:text="3"
                android:textColor="@color/holo_blue"
                android:textSize="22sp" />
        </RelativeLayout>

Now in my code I try to update the textview "current_bg_count"

private void updateBGCount(){
    try{
        RelativeLayout bgSection = (RelativeLayout) findViewById(R.id.bg_section);
        TextView bgCountTV = (TextView) bgSection.getChildAt(2);
        bgCountTV.setText(tempBG.size());
    }
    catch(Exception e){
        e.printStackTrace();
        Logger.d(TAG, "exception in updateBGCount");
    }
}

This gives a ResourceNotFountException on the line where I setText although the RelativeLayout is found without a problem. Even when I try finding it just by id like this:

TextView bgCountTV = (TextView) findViewById(R.id.current_bg_count)
bgCountTV.setText(tempBG.size());

it gives the same error. All the other views in the layout are found easily and updated just fine. Only this one TextView is giving me the problem. Does anyone have any idea what the problem is?

Upvotes: 1

Views: 447

Answers (4)

Mohit
Mohit

Reputation: 11314

the Problem is with this code

bgCountTV.setText(tempBG.size());

tempBG.size() this code is returning an int value which it is assuming as string Resource id something like R.string.VARIABLE_NAME coz it have a corresponding int value which TextView is assuming as id

What you can do

 bgCountTV.setText(tempBG.size()+"");

OR

 bgCountTV.setText(String.ValueOf(tempBG.size()));

OR

 bgCountTV.setText(tempBG.size().toString());

Upvotes: 0

Manishika
Manishika

Reputation: 5564

Try setting your tempBG.size() to string like this

bgCountTV.setText(""+tempBG.size());

This should work

Upvotes: 2

Germ&#225;n Caselles
Germ&#225;n Caselles

Reputation: 15

Are you using eclipse? Check there is no errors and warning in your project. Sometimes there are errors in xml that let you compile, but doesn't work properly.

Upvotes: 0

codeMagic
codeMagic

Reputation: 44571

You need to convert your size of the ArrayList to a String at this line

 setText(tempBG.size())

Since tempBG.size() returns an int then setText() is looking for a resource with the id of whatever that returns. You are using this setText(ResId int) method which is used for if you have a string resource that you want to use to set the text.

So change it to

setText(String.valueOf(tempBG.size()));

Upvotes: 4

Related Questions