onClick 中的 findViewById 返回空值

亚历杭德罗

所以我在适配器类中有以下功能。我希望我的 Toast 在单击某个图标时打印 TextView 的文本,但结果我无法在我的 onClick 函数中获取该 TextView。

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 但它的值发生了变化,因此没有意义(它首先被初始化为“”,然后分配了一个我从 API 获得的值)。

我试图获取的文本是在 CardView 中具有 id parentTitle 的 TextView 内部的文本。我有几个这样的卡片视图。我不知道它是否相关,但这是代码。

<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>
拉吉

做这个 -

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);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用findViewById的ListView返回空值

来自分类Dev

findViewById在Listfragment中返回null

来自分类Dev

Fragment中的findViewById返回Null

来自分类Dev

findViewById 在 AppCompatActivity 中返回 null

来自分类Dev

findViewByID 在 FirestoreRecyclerAdapter 中返回 null

来自分类Dev

一个方法从 onCreate 调用时返回空对象,但在 onClick 方法中返回正确的值

来自分类Dev

如何在 Dialog onClick 事件中访问“findViewById”方法

来自分类Dev

为什么 Activity 中的 findViewById() 不返回用户键入的值?

来自分类Dev

FindViewbyId总是在片段中返回Null

来自分类Dev

findViewById在片段类中返回null

来自分类Dev

FindViewbyId总是在Fragment中返回Null

来自分类Dev

findViewById在片段类中返回null

来自分类Dev

在片段中调用findViewById返回null

来自分类Dev

在php中为数组返回空值

来自分类Dev

Spring Boot中的查询返回空值

来自分类Dev

在Google Apps脚本中返回空值

来自分类Dev

Textarea在PHP POST中返回空值

来自分类Dev

Kurento在结果中返回空值

来自分类Dev

JavaScript中的Stringify返回空值

来自分类Dev

在 int[] 数组中返回空值

来自分类Dev

Activity.findViewById()在Espresso测试中返回null

来自分类Dev

当我在onCreate()中调用findViewById()时返回null

来自分类Dev

当我在onCreate()中调用findViewById()时返回null

来自分类Dev

findViewById中的奇怪的ClassCastException

来自分类Dev

Android Studio中的FindViewById

来自分类Dev

findViewById中的错误ListView

来自分类Dev

按钮的 onclick 中 this 的值

来自分类Dev

findViewById返回null

来自分类Dev

FindViewById返回错误的视图