List<Map<String,Object>> list 增加map对象就出错 5
packagecom.example.ppp;importjava.util.ArrayList;importjava.util.Collections;importja...
package com.example.ppp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv =(TextView)findViewById(R.id.haha);
String test="[{\"detail\":\"好好吃\",\"names\":\"蛋糕\"},{\"detail\":\"礼轻情重\",\"names\":\"礼物\"}]";
Gson gson = new Gson();
List<Map<String,Object>> list2 =gson.fromJson(test, new TypeToken<List<Map<String,Object>>>(){}.getType());
List<Map<String,Object>> list = null;
Map<String, Object> map = new HashMap<String, Object>();
map.put("haha", "haha");
map.put("names", list2.get(0).get("names").toString());
map.put("detail",list2.get(0).get("detail").toString());
//只要加上这句话就会跳出并且报错。为什么!!!!!!!!!!!!!
list.add(map);
}
} 展开
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv =(TextView)findViewById(R.id.haha);
String test="[{\"detail\":\"好好吃\",\"names\":\"蛋糕\"},{\"detail\":\"礼轻情重\",\"names\":\"礼物\"}]";
Gson gson = new Gson();
List<Map<String,Object>> list2 =gson.fromJson(test, new TypeToken<List<Map<String,Object>>>(){}.getType());
List<Map<String,Object>> list = null;
Map<String, Object> map = new HashMap<String, Object>();
map.put("haha", "haha");
map.put("names", list2.get(0).get("names").toString());
map.put("detail",list2.get(0).get("detail").toString());
//只要加上这句话就会跳出并且报错。为什么!!!!!!!!!!!!!
list.add(map);
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询