Android 在列表中保存时发生异常
我创建了一个名为Placeslist的类,当我尝试创建一个Placeslist列表并在其中保存对象时,我得到了Android.java.lang.NullPointerException 这是placeslist类Android 在列表中保存时发生异常,android,list,Android,List,我创建了一个名为Placeslist的类,当我尝试创建一个Placeslist列表并在其中保存对象时,我得到了Android.java.lang.NullPointerException 这是placeslist类 public class Placeslist { @Key public String status; @Key public String id; @Key public String name;
public class Placeslist {
@Key
public String status;
@Key
public String id;
@Key
public String name;
@Key
public String reference;
@Key
public double lat;
@Key
public double lon;
public Placeslist(String status,String id,String name,String reference,double lat,double lon) {
// TODO Auto-generated constructor stub
this.status=status;
this.id=id;
this.name=name;
this.reference=reference;
this.lat=lat;
this.lon=lon;
}
这是我尝试在列表中保存对象时的代码
Placeslist placeslist=null;
String data = EntityUtils.toString(response.getEntity());
JSONObject jsonObj = new JSONObject(data);
List<Placeslist>places=null;
JSONArray results = jsonObj.getJSONArray("results");
for (int i = 0; i < results.length(); i++) {
JSONObject result = results.getJSONObject(i);
String name = result.getString("name");
String id = result.getString("id");
String reference = result.getString("reference");
JSONObject latitudes = result.getJSONObject("geometry")
.getJSONObject("location");
double lon = latitudes.getDouble("lng");
double lat = latitudes.getDouble("lat");
placeslist=new Placeslist("OK", id, name, reference, lat, lon);
places.add(placeslist);
}
Placeslist Placeslist=null;
字符串数据=EntityUtils.toString(response.getEntity());
JSONObject jsonObj=新的JSONObject(数据);
Listplaces=null;
JSONArray results=jsonObj.getJSONArray(“结果”);
对于(int i=0;i
因为您的列表为空。修正:
List<Placeslist>places = new ArrayList<Placeslist>();
Listplaces=newarraylist();
您还可以将PlacesList重命名为Place,因为PlaceList只是列表中的一个元素。您缺少:
places = new ArrayList<PlacesList>();
places=newarraylist();
或
places=newlinkedlist();
列表
是一个接口
,不能直接用于创建对象。应该改用子类。使用debug并发布你得到的NullPointerException行。还有一条关于你的设计的评论:也许你需要将类Placeslist
重命名为Place
@Souvlaki谢谢我会更新iti得到此错误无法实例化类型列表我得到此错误无法实例化类型列表抱歉,我忘了列表只是界面。更新的版本有效。
places = new LinkedList<PlacesList>();