【问题标题】:ArrayList becomes null suddenly which was previously not nullArrayList 突然变为 null 之前不为 null
【发布时间】:2013-12-29 10:19:54
【问题描述】:

我有一个 LocationMapActivity,它使用 google map v2 来显示人员的位置(根据人员 ID 从数据库中获取位置)。我使用 异步任务 从数据库中获取位置并在地图上添加标记和路线。

我在两种情况下重新使用 LocationMapActivity(根据人员 ID 显示标记/路线):

  1. 单击 Show all people om map Button-LocationMapActivity 显示描绘每个人的标记。如果单击该标记,则通过再次向 LocationMapActivity 发送一个 Intent 到 LocationMapActivity 来显示特定人员的路线,其中人员 ID 为 put extra。

  2. 单击幻灯片放映地图按钮 - 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


【解决方案1】:

尝试更改这些变量值初始化:

private ArrayList<String> latLngCheckinTimeList = null;

只是

private ArrayList<String> latLngCheckinTimeList;

并将变量创建移至 onCreate:

private  ArrayList<LatLng> latLngArrList = new ArrayList<LatLng>();//

onCreate..

latLngArrList = new ArrayList<LatLng>();

【讨论】:

    【解决方案2】:

    这是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 
    

    亲爱的,我尽力描述这个好运:)

    【讨论】:

    • 我有时会在我的应用程序中使用它,它可以与地图一起使用 :)
    • 感谢您的回复。我按照你的建议在我的代码中使用了 DataController 类。但我仍然遇到同样的问题,但频率较低。请帮助
    • 当您使用该arraylist 时,只需验证一次arraylist!=null,如果为null,则再次重新加载数据并将它们填充到列表中:)
    • 谢谢,但我不想这样做,因为我已经根据 db 的一些标准在 arraylist 中添加了数据。另外,我会不必要地将数据重新加载到数组列表中,这是一个非常庞大的过程。我想要一个 PERSIST arraylist 的解决方案,这样它们就不会失去价值并变成空值。:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 2022-11-25
    • 2021-10-07
    • 1970-01-01
    相关资源
    最近更新 更多