Java 超类型Kotlin的直接参数不允许使用投影
我将一个Java类转换为kotlin,并得到了上面提到的错误预测,对于超类型的直接参数是不允许的 Java类是Java 超类型Kotlin的直接参数不允许使用投影,java,android,kotlin,Java,Android,Kotlin,我将一个Java类转换为kotlin,并得到了上面提到的错误预测,对于超类型的直接参数是不允许的 Java类是 public class RecipeViewHolder extends ParentViewHolder { private static final float INITIAL_POSITION = 0.0f; private static final float ROTATED_POSITION = 180f; @NonNull privat
public class RecipeViewHolder extends ParentViewHolder {
private static final float INITIAL_POSITION = 0.0f;
private static final float ROTATED_POSITION = 180f;
@NonNull
private final ImageView mArrowExpandImageView;
private TextView mRecipeTextView;
private TextView position;
private TextView total;
public RecipeViewHolder(@NonNull View itemView) {
super(itemView);
mRecipeTextView = (TextView) itemView.findViewById(R.id.recipe_textview);
position = (TextView) itemView.findViewById(R.id.textViewPosition);
total = (TextView) itemView.findViewById(R.id.textViewTotal);
mArrowExpandImageView = (ImageView) itemView.findViewById(R.id.arrow_expand_imageview);
}
public void bind(@NonNull Recipe recipe) {
mRecipeTextView.setText(recipe.getName());
try {
position.setText("Position: " + recipe.getJson().getString("position"));
total.setText("Total Bid Amount: " + recipe.getJson().getString("type"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
转换后的Kotlin类为
//Error occurs here in Parentvireholder<*,*>
class RecipeViewHolder(itemView: View) : ParentViewHolder<*, *>(itemView) {
private val mArrowExpandImageView: ImageView
private val mRecipeTextView: TextView
private val position: TextView
private val total: TextView
init {
mRecipeTextView = itemView.findViewById<View>(R.id.recipe_textview) as TextView
position = itemView.findViewById<View>(R.id.textViewPosition) as TextView
total = itemView.findViewById<View>(R.id.textViewTotal) as TextView
mArrowExpandImageView = itemView.findViewById<View>(R.id.arrow_expand_imageview) as ImageView
}
fun bind(recipe: Recipe) {
mRecipeTextView.text = recipe.name
try {
position.text = "Position: " + recipe.json!!.getString("position")
total.text = "Total Bid Amount: " + recipe.json!!.getString("type")
} catch (e: JSONException) {
e.printStackTrace()
}
}
}
//Parentvireholder中出现错误
类RecipeViewHolder(itemView:View):ParentViewHolder(itemView){
private val mArrowExpandImageView:ImageView
private val mRecipeTextView:TextView
私有val位置:TextView
私有val总计:TextView
初始化{
mRecipeTextView=itemView.findViewById(R.id.recipe\u textview)作为textview
position=itemView.findViewById(R.id.textViewPosition)作为TextView
total=itemView.findViewById(R.id.textViewTotal)作为TextView
mArrowExpandImageView=itemView.findviewbyd(R.id.arrow\u expand\u imageview)作为imageview
}
趣味绑定(配方:配方){
mRecipeTextView.text=recipe.name
试一试{
position.text=“position:”+recipe.json!!.getString(“position”)
total.text=“总投标金额:”+recipe.json!!.getString(“类型”)
}捕获(e:JSONException){
e、 printStackTrace()
}
}
}
显示的注释中出现错误。我尝试了Any修复程序,但显示类型参数不在其范围内Kotlin要求您指定超类的泛型参数 您需要用显式类型替换超类泛型类型中的
*
,或者为子类指定泛型类型
如果超类泛型类型被限制为特定类型,则不能使用Any
。查看ParentViewHolder的类定义,并声明与之相同的类型:
RecipeViewHolder(itemView:View):ParentViewHolder
或
RecipeViewHolder(itemView:View):ParentViewHolder
请提供一个ParentViewHolder
的定义。最常用的方法是以相同的方式使类泛型:RecipeViewHolder或者,如果知道父类型和子类型,则可以在扩展超类时显式指定它们,例如:RecipeViewHolder:ParentViewHolder