我将Java类转换为kotlin,并得到了以下错误:超类型的直接参数不允许进行投影
Java类是
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()
}
}
}
显示的评论中出现错误。我尝试了Any修复程序,但显示Type参数不在其范围内
答案 0 :(得分:0)
Kotlin要求您指定超类的通用参数。
您需要用显式类型替换超类泛型类型中的*
或为子类指定泛型类型。
如果超类泛型类型仅限于特定类型,则不能使用Any
。看一下ParentViewHolder的类定义,并声明与之相同的类型:
RecipeViewHolder<P extends Parent<C>, C>(itemView: View) : ParentViewHolder<P, C>
或
RecipeViewHolder(itemView: View) : ParentViewHolder<MyParent, MyChild>