简体   繁体   中英

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.

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.

I copied your code and made a dummy app. I got these results:

12-27 00:03:48.332: D/First(9165): class android.widget.TextView
12-27 00:03:48.332: D/Second(9165): class android.widget.TextView
12-27 00:03:48.332: D/Third(9165): class android.widget.ImageView

Apparently there is nothing wrong with the code

I've tried almost everything here, but nothing worked until I added a new component to the activity and, after running, noticing that the new component didn't show. It suggested my app wasn't updating with the runs. It worked by manually uninstalling the old version of the app an then running again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM