问题描述
我正在制作一个比赛应用程序,组织者可以在其中设置比赛,玩家可以在比赛中注册。我想在具有recyclerview的片段中设置countdowntimer。但是当我声明CountDownTimer变量并在方法中使用它时,出现了错误提示:
incompatible types: no unique maximal instance exists for type variable T with upper bounds CountDownTimer,View
countDownTimer = itemView.findViewById(R.id.live_time);
^
其中T是类型变量: T扩展了在方法findViewById(int)中声明的View
请帮助我!
回收站视点:
public class MyViewholder extends RecyclerView.ViewHolder {
TextView TypeTextView,ParticipantsTextView;
ImageView GameImageView;
CountDownTimer countDownTimer;
public MyViewholder(@NonNull View itemView) {
super(itemView);
TypeTextView = itemView.findViewById(R.id.live_tournament_type);
ParticipantsTextView = itemView.findViewById(R.id.live_tournament_participants);
GameImageView = itemView.findViewById(R.id.live_image_view);
countDownTimer = itemView.findViewById(R.id.live_time);
}
}
Viewholder布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<ImageView
android:id="@+id/live_image_view"
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="@drawable/live_dota_wall" />
<TextView
android:id="@+id/live_tournament_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/live_image_view"
android:layout_marginBottom="60dp"
android:text="Tournament Type: "
android:textColor="#FFFFFF"
android:textSize="20sp" />
<TextView
android:id="@+id/live_tournament_participants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/live_image_view"
android:layout_marginBottom="10dp"
android:text="Tournament \nParticipants: "
android:textColor="#FFFFFF"
android:textSize="20sp" />
<TextView
android:id="@+id/live_open_till"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@id/live_image_view"
android:layout_alignBottom="@id/live_image_view"
android:layout_marginBottom="40dp"
android:text="Resgisteration \nopen Till:"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<TextView
android:id="@+id/live_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@id/live_image_view"
android:layout_alignBottom="@id/live_image_view"
android:layout_marginRight="40dp"
android:layout_marginBottom="10dp"
android:text="TIME"
android:textColor="#FFFFFF"
android:textSize="20sp" />
解决方法
代码中的问题是您试图将TextView
对象保存在CountDownTimer
对象中,这在程序上是错误的。仅当倒数计时器是TextView的子类时,这才是正确的。
要使用倒数计时器,该语法在android文档中已非常提及 您可以检查一下
https://developer.android.com/reference/android/os/CountDownTimer