gpt4 book ai didi

Java 参数类型错误

转载 作者:行者123 更新时间:2023-12-02 03:32:49 25 4
gpt4 key购买 nike

IDE 提示:“第二个参数类型错误:找到 boolean 值,必需:?”

public void onItemClick(View view, int positon) {
HashMap<String, ?> movie = (HashMap<String, ?>) movieData.getItem(positon);

if(view.getId() == R.id.checkBox) {
boolean selection = (boolean) movie.get("selection") ? false : true;

// Error here
movie.put("selection", selection);
}
}

但是,这个可以工作

public void onItemClick(View view, int positon) {
HashMap movie = movieData.getItem(positon);

if(view.getId() == R.id.checkBox) {
boolean selection = (boolean) movie.get("selection") ? false : true;

movie.put("selection", selection);
}
}

最佳答案

Unbounded Wildcards 的 Java 教程页面中,它指出:

It's important to note that List<Object> and List<?> are not the same. You can insert an Object, or any subtype of Object, into a List<Object>. But you can only insert null into a List<?>. The Guidelines for Wildcard Use section has more information on how to determine what kind of wildcard, if any, should be used in a given situation.

您应该考虑此处使用通配符的原因。如果可能,不要使用通配符。如果您仍需要使用一个,请使用适当的 Upper Bounded Wildcard这样就可以知道类型。

由于您正在存储 Boolean在其他数据类型中,唯一的共同父级是 Object :

HashMap<String, Object> movie;

关于Java 参数类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37849707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com