Osmdroid 尝试在 Fragment.onStart()

问题描述

所以我一直在开发一个应用程序,其中包含来自 osmdroid 的地图视图,并在多边形保存到我的房间数据库时使用 LiveData 在其中绘制(新)多边形。现在,在使用地图来回循环进入片段时,地图会尝试“绘制”一个不应再存在的多边形。这个 polygon 令人惊讶地有它的私有属性 mOutline == null,因此当片段试图将它添加到地图时会抛出一个 NullPointerException。它不在我的数据库中,因为我在 Fragment.onViewCreated() 期间清除了,我还从地图中删除了所有叠加层...:

//in onViewCreated of my Fragment:
@Override
    public void onViewCreated(@NonNull View view,Bundle savedInstanceState) {
    super.onViewCreated(view,savedInstanceState);

    //...

    OverlayManager overlayManager = view_binding.map.getoverlayManager();
    overlayManager.removeAll(overlayManager.overlays());
    view_model.clearpolygon() 
    /* -> Now the database is empty (I checked it!) and the 
    * viewmodels.polygon value is set to null using postValue(null)
    * this call also is executed in another thread,because I apparantly am not allowed to
    *access my database on the main thread
    */

    view_model.polygon.observe(getViewLifecycleOwner(),polygon -> {
        // the null check is here because the Database->LiveData return 
        // null after view_model.clearpolygon()
        if (polygon != null) {
            setpolygon(polygon);
        }
    });

    //...
}

setpolygon(polygon polygon) {
    polygon.getFillPaint().setColor(Color.parseColor("#1EFFE70E"));
    view_binding.map.getoverlayManager().add(polygon);
    view_binding.map.invalidate();
    IMapController mapController = view_binding.map.getController();
    mapController.setCenter(polygon.getActualPoints().get(0));
    mapController.setZoom(16.0);    
}

现在,我使用“后退”按钮返回上一个片段并重新输入带有地图的片段,在那里我再次选择要绘制的多边形,将其写入数据库(其中包含之前被清除了!)并且片段再次从 LiveData 中绘制它...但是Fragment.onStart() 期间,当数据库为空时,它首先尝试绘制这个没有轮廓的奇怪多边形。我可以抓住 NullPointerException 但我认为我自己的代码中存在某种错误,这将是一个肮脏的解决方案。有什么线索吗?

编辑:忘记完成代码部分的注释

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...