Java 错误:(32, 27) 错误:不兼容的类型:对象无法转换为 long
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24686921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error:(32, 27) error: incompatible types: Object cannot be converted to long
提问by user3718359
I am a newbie to Java.
我是 Java 的新手。
Error:(40, 5) error: method does not override or implement a method from a supertype
Error:(32, 27) error: incompatible types: Object cannot be converted to long
错误:(40, 5) 错误:方法没有覆盖或实现来自超类型的方法
错误:(32, 27) 错误:类型不兼容:对象无法转换为长整型
at
在
@Override
public long getItemId(int position) {
String item = getItem(position);
return hashMap.get(item);
}
Full code is given below
完整代码如下
package com.javapapers.android.listview;
import android.content.Context;
import android.widget.ArrayAdapter;
import java.util.HashMap;
import java.util.List;
public class SimpleArrayAdapter extends ArrayAdapter {
Context context;
int textViewResourceId;
private static final String TAG = "SimpleArrayAdapter" ;
HashMap hashMap = new HashMap();
public SimpleArrayAdapter(Context context, int textViewResourceId,
List objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.textViewResourceId = textViewResourceId;
for (int i = 0; i < objects.size(); ++i) {
hashMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return hashMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public void add(String object){
hashMap.put(object,hashMap.size());
this.notifyDataSetChanged();
}
}
回答by azurefrog
The problem you are having stems from the fact that hashMap.get(item)
returns an Object
, but your method signature specifies that you need to return a long
.
您遇到的问题源于hashMap.get(item)
返回 a的事实Object
,但您的方法签名指定您需要返回 a long
。
public long getItemId(int position) { // you say you will return a long
String item = getItem(position);
return hashMap.get(item); // but try to return an Object
}
There is no way in Java for the JVM to automaticallyconvert any random Object
into a long
, so the compiler is giving you that error.
Java 中没有办法让 JVM自动将任何 randomObject
转换为 a long
,因此编译器会给你那个错误。
There are a couple ways to fix this.
有几种方法可以解决这个问题。
The Wrong Way
错误的方法
The first (bad) way is castthe variable you are getting from the map to long
like so:
第一种(坏)方法是将您从地图中获取的变量转换为long
如下所示:
public long getItemId(int position) { // you say you will return a long
String item = getItem(position);
return (long)hashMap.get(item); // and you return a long
}
This will make your code compile, but effectively what you are doing here is promising the compiler that you are really, reallysure that what you put in the Map
is a Long
. At this point the compiler will attempt to unbox the Long
into a long
and return it. If it really was a Long
this will work, and if not you will get a ClassCastException
thrown.
这将使您的代码编译,但实际上您在这里所做的是向编译器保证您确实,非常确定您放入Map
的Long
. 此时,编译器将尝试拆箱Long
成long
和返回。如果它真的是一个,Long
这将起作用,否则你会被ClassCastException
抛出。
The Right Way
正确的方式
In newer versions of Java, there is something called Generics. Using Generics you can specify the allowed type of object which can be added to a container when you define it, like so:
在较新版本的 Java 中,有一种叫做泛型的东西。使用泛型,您可以指定允许的对象类型,在定义容器时可以将其添加到容器中,如下所示:
// create a HashMap where all keys are Objects and all values are Longs
Map<Object, Long> hashMap = new HashMap<>();
public long getItemId(int position) { // you say you will return a long
String item = getItem(position);
return hashMap.get(item); // no cast is required
}
Now, the compiler will only allow values of type Long
to be stored in your map, and anything you get()
from it will automatically be a Long
, removing the need (and danger) of casting the return type.
现在,编译器将只允许 type 的值Long
存储在您的地图中,并且您get()
从中获得的任何内容都将自动成为 a Long
,从而消除了转换返回类型的需要(和危险)。
回答by mick88
First of all you should declare type params in your hashmap like so:
首先,您应该在哈希图中声明类型参数,如下所示:
HashMap<String, Long> hashMap = new HashMap<String, Long>();
Then you should be able to cast the hash map value to long implicitly without a problem.
然后,您应该能够毫无问题地将哈希映射值隐式转换为 long。
回答by blh83
The first error is telling you that the function "getItemId(int position)" does not exist anywhere else in your inheritance tree (namely above your current file). Therefore, you don't want the "@override" before the function name.
第一个错误是告诉您函数“getItemId(int position)”在继承树中的其他任何地方都不存在(即在当前文件之上)。因此,您不希望在函数名称之前出现“@override”。
Also, your hashmap does not declare the type of data it holds. When you call hashmap.getItem(position) it is returning an object of some type and your function definition says it returns a long, therefore you need to be sure your hashmap contains either longs or something that can be converted to a long, and that is what is returned from getItem.
此外,您的哈希图没有声明它所持有的数据类型。当您调用 hashmap.getItem(position) 时,它返回某个类型的对象,并且您的函数定义说它返回一个 long,因此您需要确保您的 hashmap 包含 long 或可以转换为 long 的内容,并且是从 getItem 返回的内容。