findViewById 返回错误的元素?

发布于 2024-12-22 20:27:09 字数 2422 浏览 1 评论 0原文

我不明白为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染年凉城似染瑾 2024-12-29 20:27:09

尝试重新加载您的项目,可能是 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.

请止步禁区 2024-12-29 20:27:09

我复制了你的代码并制作了一个虚拟应用程序。我得到这些结果:

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

显然代码没有任何问题

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

祁梦 2024-12-29 20:27:09

我已经尝试了这里几乎所有的方法,但是没有任何效果,直到我向活动添加了一个新组件,并且在运行后,注意到新组件没有显示。它表明我的应用程序没有随着运行而更新。它的工作原理是手动卸载旧版本的应用程序,然后再次运行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文