Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findViewById retrurns wrong element?

I can't understand why findViewById returns the wrong element, here is the class:

public class EventDetailsFragment extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.event_details);
        Log.d("First", findViewById(R.id.tuxtView1).getClass().toString());
        Log.d("Second", findViewById(R.id.tuxtView2).getClass().toString());
        Log.d("Third", findViewById(R.id.imageView1).getClass().toString());
    }   
}

And here is the xml:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp">

    <RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="0dp"
    android:layout_height="175dp"
    android:layout_gravity="center_horizontal"
    android:layout_weight="1"
    android:background="#fff"
    android:gravity="right" >

        <ImageView
        android:id="@+id/imageView1"
        android:layout_width="110dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:src="@drawable/ic_event_image" />

        <TextView
        android:id="@+id/tuxtView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp" />      
        <TextView
        android:id="@+id/tuxtView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tuxtView2"
        android:layout_toRightOf="@+id/imageView1"
        android:textColor="#555"
        android:textSize="20dp" />

    </RelativeLayout>

</LinearLayout>

The logcat ouput is:

12-26 23:43:20.249: D/First(6789): class android.widget.TextView
12-26 23:43:20.249: D/Second(6789): class android.widget.ImageView
12-26 23:43:20.249: D/Third(6789): class android.widget.TextView

So the point is, why do I get a imageview with id R.id.tuxtView2 and a textview with id R.id.imageView1. The application crashes if I want to assign a text value to R.id.tuxtView2, casted as a TextView.

like image 312
jonepatr Avatar asked Dec 26 '11 22:12

jonepatr


1 Answers

Try to reload your project, maybe something wrong with R.java, or verify you are calling the good file with setContentView.

By refreshing/cleaning your project the R.java file will be reloaded and will find the named widgets.

like image 50
Jeremy D Avatar answered Oct 05 '22 20:10

Jeremy D