我从一个巨大的Jsonfile创建了一个非常简单的自定义对象列表。这个简单的列表包含了原始解析的JSON的大约1/8到1/10(大约400 kb左右,完整版是3mb),我可以扔掉它。我只使用我在这个列表中需要的数据,所以如果支持两种语言,在我的简单列表中,我只列出了我需要的数据。这个列表我序列化,所以我不需要在启动时分析所有的数据。没有封闭式的错误:它是如何工作的
该对象应该包含一些纯数据和两个带有视图自定义对象的列表。但我试图填补这些子列表时,我的问题发生。该错误是No enclosing instance of type SimpleVenue is accessible. Must qualify the allocation with an enclosing instance of type SimpleVenue (e.g. x.new A() where x is an instance of SimpleVenue).
我想我理解它,所以我创建一个顶对象,包含三个对象:
- 与明文数据
- 与两个URL(串)的对象的第一列表对象它
- 其中包含两个字符串和两个日期对象的第二个对象列表。
实际上额外的水平感觉有点不必要,我希望这两个列表对象是普通数据对象的子对象。 任何人都知道我需要改变什么?
我的课:
import java.io.Serializable;
import java.util.Date;
import java.util.List;
public class SimpleVenue implements Serializable, Comparable<SimpleVenue>{
/**
*
*/
private static final long serialVersionUID = 1L;
public SimpleVenueData simplevenuedata;
public List <SimplePhotos> simplephotos;
public List <SimpleOpeningHours> openinghours;
public class SimpleVenueData implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public int ID;
public String name;
public String category;
public String address;
public String city;
public String tip;
public String phone;
public String website;
public String foursquare;
public float latitude;
public float longtitude;
public String theme;
public String icon;
public String exception;
//setters
public void setId(int id){
ID=id;
}
public void setName(String vname){
name=vname;
}
public void setCategory(String icon){
if(icon.equals("eten.png") || icon.equals("uitgaan.png")){
category="food";
}else if(icon.equals("monument.png")){
category="sights";
}else if(icon.equals("winkelen.png")){
category="shops";
}
}
public void setAdress(String vaddress){
address = vaddress;
}
public void setCity(String vcity){
city=vcity;
}
public void setTip(String vtip){
tip=vtip;
}
public void setPhone(String vphone){
phone=vphone;
}
public void setWebsite(String vwebsite){
website=vwebsite;
}
public void setFoursquare(String vfoursquare_link){
foursquare=vfoursquare_link;
}
public void setLatitude(float vlatitude){
latitude=vlatitude;
}
public void setLongitude(float vlong){
longtitude=vlong;
}
public void setTheme(String vtheme){
theme=vtheme;
}
public void setIcon(String vicon){
icon=vicon;
}
public void setException(String vexception){
exception = vexception;
}
}
public class SimplePhotos implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public String medium;
public String large;
public void setMedium(String vmedium){
medium=vmedium;
}
public void setLarge(String vlarge){
large=vlarge;
}
public String getMedium(){
return medium;
}
public String getLarge(){
return large;
}
}
public class SimpleOpeningHours implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public String day;
public boolean closed;
public Date starttime;
public Date endtime;
//setters
public void setDay(String vday){
day=vday;
}
public void setClosed(boolean vclosed){
closed=vclosed;
}
public void setStartime(Date vstarttime){
starttime=vstarttime;
}
public void setEndime(Date vendtime){
endtime=vendtime;
}
//getters
public String getDay(){
return day;
}
public boolean isClose(){
return closed;
}
public Date getStarttime(){
return starttime;
}
public Date getEndtime(){
return endtime;
}
}
@Override
public int compareTo(SimpleVenue another) {
// XXX Auto-generated method stub
return 0;
}
}
时使用:
List<SimpleVenue> sv = new ArrayList<SimpleVenue>();
int selectedPic;
JJsonResponse e;
int k =0; //indicator 1e SimpleVenue
JVenueThemes jtheme;
SimpleVenue tempSv;
SimpleVenueData tempSvd;
SimplePhotos tempPhoto;
int selectedCounter;
for(int i=0;i < jsonResponse.size() ;i++){
e = jsonResponse.get(i);
if(e.venue.hidden == false){ //staat aan
for(int j=0; j<e.venue.themes.size();j++){ //loop door alle themes
if (e.venue.themes.get(j).mobile == true){ //als theme is true
jtheme = e.venue.themes.get(j);
sv.add(new SimpleVenue());
tempSv=sv.get(k);
tempSvd=sv.get(k).simplevenuedata;
tempSvd.setId(k);
tempSvd.setName(e.venue.name);
tempSvd.setAdress(e.venue.address);
tempSvd.setCity(e.venue.city);
tempSvd.setPhone(e.venue.phone);
tempSvd.setWebsite(e.venue.website);
tempSvd.setFoursquare(e.venue.foursquare_link);
tempSvd.setLatitude(e.venue.latitude);
tempSvd.setLongitude(e.venue.longitude);
tempSvd.setCategory(jtheme.icon);
tempSvd.setIcon(jtheme.icon);
// language depending
if (language.equalsIgnoreCase("nld")){ //dutch
tempSvd.setTip(e.venue.tip);
tempSvd.setTheme(jtheme.name);
}else{ //english
tempSvd.setTip(e.venue.tip_en);
tempSvd.setTheme(jtheme.name_en);
}
// selecting pics
if (e.venue.venue_photos.isEmpty() == false){
for(int l=0; l < e.venue.venue_photos.size() ;l++){
selectedCounter=0;
if(e.venue.venue_photos.get(l).selected == true){
sv.get(k).simplephotos.add(new SimplePhotos());
// tempSv.simplephotos.add(new SimplePhotos());
// tempSv.simplephotos.get(selectedCounter).large=e.venue.venue_photos.get(selectedCounter).large;
// tempSv.simplephotos.get(selectedCounter).medium=e.venue.venue_photos.get(selectedCounter).medium;
// selectedCounter++;
}
}
}else{
tempSv.simplephotos.add(new SimpleVenue.SimplePhotos());
tempSv.simplephotos.get(0).large=null;
tempSv.simplephotos.get(0).medium=null;
}
// selecting opening hours
//testrun before entering opening hours
Log.i("testrun", "k= " + String.valueOf(k));
k++;
}
}
}
jsonResponse.remove(i); //
}
我甚至没有开始使其可比性,所以不介意。但最终我会在名单和主题等项目上列出我的名单。
不,我没有,现在我把它们放在他们自己的班里。感谢你帮助我。现在它在运行之前不会导致错误,但是现在当我添加新图片时,即使它在此行上为空时,我也有一个nullpointerexception:tempSv.photos.add(new SimplePhotos());你知道为什么吗? (其中tempsv = simplevenue项目(就像我之前的文章中的simplevenuedata)) – Jasper 2012-07-27 11:59:35
@Jasper:你的文章包含了太多的代码,我现在就去查看它。我建议你为此提出一个单独的问题,把它缩减为一个简短但完整的程序,显示*只是让你困惑的那一点。我怀疑,通过这个例子,你会发现什么是错的。 – 2012-07-27 12:38:02