如何在获取'无法解析符号'错误时将键和值添加到Hashmap中 [英] How to add keys and values to a Hashmap while getting 'cannot resolve put symbol' error

查看:1022
本文介绍了如何在获取'无法解析符号'错误时将键和值添加到Hashmap中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android Studio 1.4.1,并且刚刚创建了一个Hashmap,并且正在关注如何填充和操作它的教程(在Java上)。
但是,我得到'无法解决符号放置'错误,put命令是红色的。
我添加的图像显示了自动完成快照,虽然导入了java.util.HashMap,但没有自动完成中可用的put命令。可用命令也显示为红色。我试图用它们代替put命令。我一直都有这种类型的问题。谁能帮忙?
预先感谢您...

  import java.util.HashMap; 

HashMap< String,String> pozisyon = new HashMap< String,String>();
pozisyon.put(SKale,a8);


解决方案

EDIT1:
方法外部的HashMap字段。这样的事情不会奏效:

  public class Class {
HashMap< String,String> hashMap = new HashMap< String,String>();
hashMap.put(one,two);





$ b

如果你想达到这个目的,把它放在构造函数中,就像这样:

  public class Class {
HashMap< String,String> hashMap = new HashMap< String,String>();

public Class(){
hashMap.put(one,two);
}
}

其他方法可以在 static 块。

I am working with Android Studio 1.4.1 and I had just created a Hashmap and was following a tutorial (on java) on how to populate and manipulate it. However I get a 'cannot resolve symbol put' error and the "put" command is in red. The image I added shows the auto complete snapshot and although java.util.HashMap is imported there is no "put" command that is available in autocomplete. The available commands also are showing in red. I tried to use them instead of the "put" command. I keep having this type of problem all along. Can anyone help? Thank you in advance...

import java.util.HashMap;

HashMap<String, String> pozisyon = new HashMap<String, String>();
pozisyon.put("SKale", "a8");

解决方案

EDIT1: You cannot add elements in HashMap fields outside of methods. Things like this wont work:

public class Class {
    HashMap<String, String> hashMap = new HashMap<String, String>();
    hashMap.put("one", "two");
}

If you want to achieve that, put it in the constructors, like so:

public class Class {
    HashMap<String, String> hashMap = new HashMap<String, String>();

    public Class() {
        hashMap.put("one", "two");
    }
}

Other way you can do it is in a static block.

这篇关于如何在获取'无法解析符号'错误时将键和值添加到Hashmap中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆