【问题标题】:Error:(32, 27) error: incompatible types: Object cannot be converted to long错误:(32、27)错误:不兼容的类型:对象无法转换为长
【发布时间】:2014-07-10 21:51:26
【问题描述】:

我是 Java 新手。

错误:(40, 5) 错误:方法未覆盖或实现超类型中的方法
错误:(32, 27) 错误:不兼容的类型:对象无法转换为长型

    @Override
    public long getItemId(int position) {
        String item = getItem(position);
        return hashMap.get(item);
    }

完整代码如下

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();
    }
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    您遇到的问题源于hashMap.get(item) 返回Object,但您的方法签名指定您需要返回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
    }
    

    在 Java 中,JVM 无法自动将任何随机的 Object 转换为 long,因此编译器会给出该错误。

    有几种方法可以解决此问题。

    错误的方式

    第一种(不好的)方法是 cast 您从地图中获取的变量到 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
    }
    

    这将使您的代码编译,但实际上您在这里所做的是向编译器保证您是真的,真的确保您放入 Map 的内容是 Long。此时,编译器将尝试将Long 拆箱为long 并返回它。如果它真的是 Long,这将起作用,如果不是,您将得到 ClassCastException 抛出。

    正确的方式

    在较新版本的 Java 中,有一个叫做 Generics 的东西。使用泛型,您可以指定允许的对象类型,当您定义它时可以将其添加到容器中,如下所示:

    // 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
    }
    

    现在,编译器将只允许将 Long 类型的值存储在您的映射中,而您从其中获得的任何 get() 都将自动成为 Long,从而消除了强制返回的需要(和危险)输入。

    【讨论】:

      【解决方案2】:

      首先你应该像这样在你的 hashmap 中声明类型参数:

      HashMap<String, Long> hashMap = new HashMap<String, Long>();
      

      那么您应该能够将哈希映射值隐式转换为 long 而不会出现问题。

      【讨论】:

        【解决方案3】:

        第一个错误是告诉您函数“getItemId(int position)”在您的继承树的任何其他位置(即当前文件上方)不存在。因此,您不希望在函数名称前加上“@override”。

        此外,您的 hashmap 没有声明它所保存的数据类型。当您调用 hashmap.getItem(position) 它返回某种类型的对象并且您的函数定义说它返回一个 long,因此您需要确保您的 hashmap 包含 longs 或可以转换为 long 的东西,并且是从 getItem 返回的内容。

        【讨论】:

          猜你喜欢
          • 2017-11-09
          • 1970-01-01
          • 1970-01-01
          • 2021-04-27
          • 1970-01-01
          • 2023-03-26
          • 1970-01-01
          • 1970-01-01
          • 2018-05-28
          相关资源
          最近更新 更多