在onClick内的findViewById返回null值

时间:2018-06-03 20:58:33

标签: java android

所以我在适配器类中有以下功能。我希望我的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内部的文本。我有几个这样的cardViews。我不知道它是否相关,但这里是代码。

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

2 个答案:

答案 0 :(得分:2)

这样做 -

import graphics as g

def main():

    win = g.GraphWin("Draw a polygon")
    win.setCoords(0.0, 0.0, 100.0, 100.0)
    message = g.Text(g.Point(50, 50), "Click to add point to polygon")
    message.draw(win)

    # Set of points clicked so far
    points = []
    def onClick(pt):
        x, y = win.toWorld(pt.x, pt.y)
        points.append(g.Point(x, y))
        poly = g.Polygon(*points)

        # Clear all objects on canvas.
        # You can choose to delete only current polygon by associating a label with it.
        # See Tkinter's documentation for details
        win.delete("all")

        poly.setFill("Red")
        poly.setOutline("Cyan")
        poly.draw(win)

    win.setMouseHandler(onClick)

    # This is not idea as we are wasting cycles doing polling
    # I believe Tkinter should have a better approach to avoid this.
    while not win.checkKey() == 'q':
        pass


if __name__ == "__main__":
    main()

答案 1 :(得分:0)

playIcon.setOnClickListener

之前写下面的声明代码
TextView rutNameView = (TextView) v.findViewById(R.id.parentTitle);