首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >onClick内部的findViewById返回空值

onClick内部的findViewById返回空值
EN

Stack Overflow用户
提问于 2018-06-04 04:58:33
回答 2查看 38关注 0票数 0

因此,我在适配器类中有以下函数。我希望Toast在单击某个图标时打印TextView的文本,但事实证明我无法在onClick函数中获得该TextView。

代码语言:javascript
复制
public TitleParentViewHolder onCreateParentViewHolder(ViewGroup viewGroup) {

    View view = inflater.inflate(R.layout.list_routines, viewGroup, false);
    ImageView playIcon = (ImageView) view.findViewById(R.id.playRoutine);
    playIcon.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            TextView rutNameView = (TextView) v.findViewById(R.id.parentTitle);
            String rutName = rutNameView.getText().toString(); //THIS crashes because rutNameView is null
            Toast.makeText(v.getContext(), rutName + " was played",
                    Toast.LENGTH_LONG).show();

        }
    });
    return new TitleParentViewHolder(view);
}

我曾尝试在onClick之外定义rutName,但它的值发生了变化,因此没有任何意义(它首先被初始化为"“,然后赋值给我从onClick获得的值)。

我要获取的文本是TextView中id为parentTitle的CardView中的文本。我有几个这样的cardViews。我不知道它是否相关,但这是代码。

代码语言:javascript
复制
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/playRoutine"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentLeft="true"
            android:layout_margin="8dp"
            android:src="@drawable/play_gris"
            android:visibility="visible" />

        <TextView
            android:id="@+id/parentTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:padding="16dp"
            android:textSize="20dp"
            android:textStyle="bold" />

        <ImageButton
            android:id="@+id/expandArrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_margin="8dp"
            android:src="@drawable/expande_de_lista"
            android:visibility="visible" />
    </RelativeLayout>

</android.support.v7.widget.CardView>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-04 05:14:10

这样做-

代码语言:javascript
复制
public TitleParentViewHolder onCreateParentViewHolder(ViewGroup viewGroup) {

View view = inflater.inflate(R.layout.list_routines, viewGroup, false);
ImageView playIcon = (ImageView) view.findViewById(R.id.playRoutine);
TextView rutNameView = (TextView) view.findViewById(R.id.parentTitle);
playIcon.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        String rutName = rutNameView.getText().toString();
        Toast.makeText(v.getContext(), rutName + " was played",
                Toast.LENGTH_LONG).show();

    }
});
return new TitleParentViewHolder(view);
}
票数 2
EN

Stack Overflow用户

发布于 2018-06-04 05:03:27

playIcon.setOnClickListener之前编写以下声明代码

代码语言:javascript
复制
TextView rutNameView = (TextView) v.findViewById(R.id.parentTitle);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50670912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档