我有一个 LocationMapActivity ,它使用 google map v2 来显示人员的位置(根据人员ID从数据库中提取位置)。我使用异步任务< / strong>从数据库中获取位置并在地图上添加标记和路径。
我在两种情况下使用LocationMapActivity(根据人物ID显示标记/路线):
点击显示所有人的地图Button-LocationMapActivity显示描绘每个人的标记。如果单击标记,则再次向LocationMapActivity发送意图并将人员ID作为额外发送,显示特定人的路线。 / p>
点击幻灯片放映地图按钮 - 位置地图活动首先显示描绘所有人的所有标记。然后在10秒之后,它重新呈现每个人的地图和显示路线(一个接一个,间隔10秒)。请参阅我的代码的已接受答案:How to place Multiple MapViews v1 inside a ViewFlipper。
问题:在方案1中,一切都运行良好。但在方案2中,ArraryList latLngArrlist损失值 突然!我调试了代码,数组列表永远不会设置为null 它随机变为空。这些arrayList是私有的,但不是本地的 变量
注意::在渲染下一个地图视图之前,我使用重置功能清除所有的arraylists和非局部变量。
我的代码非常冗长,所以我只在下面发布相关的代码:
LocationMapActivity
public class LocationMapActivity extends Activity implements OnClickListener,
OnMarkerClickListener {
private ArrayList<String> latLngCheckinTimeList = null;//
private ArrayList<String> addressList = null;//
private ArrayList<LatLng> latLngArrList = new ArrayList<LatLng>();//
private ArrayList<Long> checkinTimeArrList = new ArrayList<Long>();//
private String selectedFieldOfficerId;
private GoogleMap googleMap;
private List<List<HashMap<String, String>>> route = null;
private HashMap<Marker, String> markerHmap = new HashMap<Marker, String>();//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity_screen);
context = this;
//person ID according to which will be displayed
selectedFieldOfficerId = getIntent().getStringExtra(
AllConstants.FOIdKey);
latLngCheckinTimeList = new ArrayList<String>();
//my code
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.mapView)).getMap();
mapTask = new MapTask(context, selectedFieldOfficerId, this);
mapTask.execute();// async task for url of route
}
googleMap.setOnMarkerClickListener(this);
}
}
MapTask异步任务
@Override
protected Void doInBackground(Void... params) {
helper = new EmployeeHelper(getApplicationContext());
if (!fieldOfficerId.equals(AllConstants.ShowAllFOInMap)
&& !fieldOfficerId.equals(AllConstants.ShowAllFOInMapSlide)) {
// map for specific fo
showSpecificFoMap(fieldOfficerId);
} else if (fieldOfficerId.equals(AllConstants.ShowAllFOInMap)) {
// show all emp in map
showAllFoOnMap((AllConstants.ShowAllFOInMap));
} else if (fieldOfficerId.equals(AllConstants.ShowAllFOInMapSlide)) {
// show slide show
//PROBLEM HERE
Log.i("map", "in slide show if ");
ArrayList<String> foIdList = new ArrayList<String>();
Boolean condition = true;
while (condition && !isFinishActivity) {
// runs only once
Log.i("map", "in slide show if WHILE");
mapScreenshotsNo = 1;
try {
foIdList = showAllFoOnMap((AllConstants.ShowAllFOInMapSlide));//ARRAYLIST BECOMES NULL IN THIS FUNCTION
} catch (Exception e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
public void run() {
if (progressDialog != null
&& progressDialog.isShowing()
&& !isFinishActivity)
progressDialog.dismiss();
}
});
try {
util.haveASleep(slideShowInterval);
} catch (Exception e) {
e.printStackTrace();
}
resetVariables();
mapScreenshotsNo++;
if (foIdList != null) {
try {
mapScreenshotsNo = showEachFOMap(foIdList,
mapScreenshotsNo);
} catch (Exception e) {
e.printStackTrace();
}
resetVariables();
}
condition = false;
}
if (mapTask != null)
mapTask.cancel(true);
// mapActivity.finish();
}
return null;
}
我理解Arraylists正在失去价值。但我不知道如何在持久性内存中保存ArrayList类型,因为它不是主要类型。几天之后我就一直在努力解决这个问题。
编辑:使用任何设置模式,ArrayList突然变为空。有时,代码在arraylists中给出空指针时运行正常。通常,空指针由ArrayList latLngArrList
引起请帮忙!!!
感谢。
答案 0 :(得分:0)
尝试更改这些变量值:
private ArrayList<String> latLngCheckinTimeList = null;
到
private ArrayList<String> latLngCheckinTimeList;
将变量创建移动到onCreate:
private ArrayList<LatLng> latLngArrList = new ArrayList<LatLng>();//
的onCreate ..
latLngArrList = new ArrayList<LatLng>();
答案 1 :(得分:-1)
这是datacontroller类,它是单例类的意思是只有单个对象或单个实例的类,我们称之为单例类。
public class DataController {
private static DataController ref;
public static DataController getInstance()
{
if(ref==null)
ref = new DataController();
return ref;
}
public void deleteInstance()
{
ref=null;
}
//here we can declare any type of data either it is primitive type or derived type data like this
public ArrayList<CommanWrapperForListing> arraylist_wrapper_datacntrlr = new ArrayList<CommanWrapperForListing>();
public boolean iswifi = true;
public HashMap<String, ArrayList<CommanWrapperForListing>>caching = new HashMap<String, ArrayList<CommanWrapperForListing>>();
public HashMap<String, HashMap<String, ArrayList<CommanWrapperForListing>>>caching_machine = new HashMap<String, HashMap<String, ArrayList<CommanWrapperForListing>>>();
// now the place where u receive or fill arraylist then use this like
DataController datacntrl; // global declare this
public ArrayList<CommanWrapperForListing> arraylist_wrapper_local = new ArrayList<CommanWrapperForListing>();//global same as datacotrller have
datacntrl = DataController.getInstance(); // in oncreate()
//then fill your arraylist which is same as u declare in datacontroller getting my point means both are same type of arraylist
//then just put this data type into the datacontroller class like this
datacntrl.arraylist_wrapper_datacntrlr .add(new CommanWrapperForListing(new File(path).getName(), path, "Audio",false,color_string));
//or you can also do this
controller.arraylist_wrapper_datacntrlr = arraylist_wrapper_local ;//once you fill this local arraylist then put it into datacontroller class varible
//now just use this datacontroller arraylist it will never loss untill n unless u not delete it into stack
//use like
//datacntrl.arraylist_wrapper_datacntrlr ; as we use static member of any class
我尽力描述这件好运,亲爱的:)