【问题标题】:Projections are not allowed for immediate arguments of a supertype Kotlin超类型 Kotlin 的直接参数不允许投影
【发布时间】:2019-05-02 10:16:46
【问题描述】:

我将一个 Java 类转换为 kotlin,并收到提到的错误 Projections are not allowed for immediate arguments of a supertype

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 参数不在其范围内

【问题讨论】:

  • 请提供ParentViewHolder的定义

标签: java android kotlin


【解决方案1】:

Kotlin 要求您指定超类的通用参数。

您需要将超类泛型类型中的* 替换为显式类型或为子类指定泛型类型。

如果超类泛型类型仅限于特定类型,则不能使用Any。查看 ParentViewHolder 的类定义并声明与它相同的类型:

RecipeViewHolder&lt;P extends Parent&lt;C&gt;, C&gt;(itemView: View) : ParentViewHolder&lt;P, C&gt;

RecipeViewHolder(itemView: View) : ParentViewHolder&lt;MyParent, MyChild&gt;

【讨论】:

  • 最通用的方法是以相同的方式使您的类通用:RecipeViewHolder

    , C> 或者如果您知道父类型和子类型,则可以在扩展时明确指定它们超类,例如:RecipeViewHolder : ParentViewHolder

猜你喜欢
  • 2017-10-18
  • 1970-01-01
  • 1970-01-01
  • 2018-06-18
  • 2019-04-05
  • 1970-01-01
  • 1970-01-01
  • 2014-09-18
  • 1970-01-01
相关资源
最近更新 更多