【问题标题】:Wrong 1st Arguement type. Found 'String' , required Integer第一个参数类型错误。找到 'String' ,需要 Integer
【发布时间】:2017-03-02 08:36:06
【问题描述】:

我正在尝试将一个 json 数组放入 Recyclerview。当我只使用一个论点(store_textposition)时,一切正常,但是当我添加另一个论点(store_name)时,它给了我"Wrong 1st argument type. Found 'String' , required Integer"

这是我的 json 数组:

{"action":"true","error":"","data":[{"_id":"58ad8d8ca49d0e11e21c4504","store_name":"firstStore","store_view":0,"store_textposition":null}]}

我在哪里得到错误:

 private boolean parse()
{
    try
    {
        JSONObject obj = new JSONObject(jsonData);
        JSONArray ja = obj.getJSONArray("data");
        JSONObject jo;
        shops.clear();
        for(int i=0;i<ja.length();i++)
        {
            jo=ja.getJSONObject(i);
            String store_name = jo.getString("store_name");
            String store_textposition = jo.getString("store_textposition");
            shops.add(store_textposition,store_name);
        }
        return true;
    }
    catch (JSONException e)
    {
        e.printStackTrace();
        return false;
    }
}

【问题讨论】:

  • 我不明白。这里有什么不清楚的地方?该错误明确表示第一个参数需要是一个整数,但你的是一个字符串。
  • shops 对象有什么类型?
  • 我假设shopsList&lt;String&gt;。我还将假设您正在尝试在列表中添加两个字符串。正确的方法是调用 add() 两次,每次调用每个字符串。编译器认为您正在尝试使用 add(index, item) 方法将字符串项添加到指定索引。
  • @AlinPandichi 我试过了,但它在不同的 recyclreviews 中显示了每个争论
  • shops 是你的模型类吗?

标签: java android json


【解决方案1】:

正如@AlinPandichi 在 cmets 中提到的,您的问题是当您将对象添加到购物清单时。

我不知道你到底想做什么。但是,如果 store_textposition 是一个 int,你最好使用

int store_textposition = jo.optInt("store_textposition");

这样,即使服务器传递你的 null 你也有默认值 0 这可以防止你的应用程序崩溃。

我想它会解决你的问题。

【讨论】:

    猜你喜欢
    • 2019-07-17
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 2019-03-29
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多