【发布时间】:2013-12-29 10:19:54
【问题描述】:
我有一个 LocationMapActivity,它使用 google map v2 来显示人员的位置(根据人员 ID 从数据库中获取位置)。我使用 异步任务 从数据库中获取位置并在地图上添加标记和路线。
我在两种情况下重新使用 LocationMapActivity(根据人员 ID 显示标记/路线):
单击 Show all people om map Button-LocationMapActivity 显示描绘每个人的标记。如果单击该标记,则通过再次向 LocationMapActivity 发送一个 Intent 到 LocationMapActivity 来显示特定人员的路线,其中人员 ID 为 put extra。
单击幻灯片放映地图按钮 - LocationMapActivity 首先显示描绘所有人的所有标记。然后在 10 秒后重新渲染地图并显示每个人的路线(以 10 秒的间隔逐一显示)。请参阅我的代码的已接受答案:How to place Multiple MapViews v1 inside a ViewFlipper。
问题: 在场景 1 中一切正常。但在场景 2 中,ArraryList latLngArrlist 损失值 突然!。我已经调试了数组列表永远不会设置为空的代码 它随机变为空。这些 arrayList 是私有的,但不是本地的 变量。
注意::在渲染下一个地图视图之前,我使用了一个重置函数来清除所有的数组列表和非局部变量。
我的代码很长,所以我只在下面发布相关的代码:
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;
}
我了解 Arraylist 正在失去价值。但我不知道如何将 ArrayList 类型保存在持久内存中,因为它不是原始类型。几天以来我一直在努力解决这个问题。
EDIT :ArrayList 突然变为空,任何设置模式。有时代码运行良好,有时它在arraylists 处给出空指针。通常空指针是由ArrayList latLngArrList引起的
请帮忙!!!
谢谢。
【问题讨论】:
-
他们知道单例类
-
@Bhanu 不,我不知道单身人士。请指导
-
k 所以你只想得到数组的值,并希望将该值存储在安全的地方,这是你的问题,亲爱的
-
@RachitaNanda 嘿 rachita 你找到解决方案了吗?
-
@partik no ..但是一旦我将私有 ArrayList
List 更改为 ArrayList List 。即删除私有。然后问题没有再次发生。虽然它不是一个正确的解决方案
标签: android arraylist android-asynctask nullpointerexception google-maps-android-api-2