Java onClick retuns空值内的findViewById
因此,我在适配器类中具有以下功能。我希望我的Toast在单击某个图标时打印文本视图的文本,但结果是我无法在onClick函数中获取该文本视图Java onClick retuns空值内的findViewById,java,android,Java,Android,因此,我在适配器类中具有以下功能。我希望我的Toast在单击某个图标时打印文本视图的文本,但结果是我无法在onClick函数中获取该文本视图 public TitleParentViewHolder onCreateParentViewHolder(ViewGroup viewGroup) { View view = inflater.inflate(R.layout.list_routines, viewGroup, false); ImageView playIcon =
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>
在播放图标之前写下声明代码。setOnClickListener
TextView rutNameView = (TextView) v.findViewById(R.id.parentTitle);
在playIcon.setOnClickListener之前写下声明代码
TextView rutNameView = (TextView) v.findViewById(R.id.parentTitle);
这样做-
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);
}
这样做-
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);
}