findViewById 返回错误的元素?
我不明白为什么 findViewById 返回错误的元素,这里是类:
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());
}
}
这是 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>
logcat 输出是:
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
所以重点是,为什么我得到一个 id 为 R.id.tuxtView2 的 imageview 和一个带有 id 的 textview id R.id.imageView1。如果我想将文本值分配给 R.id.tuxtView2(转换为 TextView),应用程序会崩溃。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试重新加载您的项目,可能是 R.java 有问题,或者验证您是否使用 setContentView 调用了正确的文件。
通过刷新/清理项目,R.java 文件将重新加载并找到指定的小部件。
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:
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.