这里地图MapView在Fragment中不起作用

时间:2018-05-25 07:22:26

标签: android here-api

我正在尝试在片段中实施 Here Maps ,但它不起作用。我有很多崩溃

崩溃日志:

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
        Suppressed: java.lang.Throwable: HERE SDK Version: 3.7.0.118
        at com.nokia.maps.MapsEngine$l.uncaughtException(MapsEngine.java:378)
        at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1068)
        at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1063)
        Suppressed: java.lang.Throwable: HERE SDK Version: 3.7.0.118
        at com.nokia.maps.MapsEngine$l.uncaughtException(MapsEngine.java:378)
        at com.nokia.maps.MapsEngine$l.uncaughtException(MapsEngine.java:379)
        at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1068)
        at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1063)
     Caused by: com.here.android.mpa.common.UnintializedMapEngineException: Cannot create HERE SDK object before MapEngine is initialized. See MapEngine.init()
        at com.nokia.maps.BaseNativeObject.u(BaseNativeObject.java:39)
        at com.nokia.maps.BaseNativeObject.<init>(BaseNativeObject.java:26)
        at com.nokia.maps.MapImpl.<init>(MapImpl.java:424)
        at com.here.android.mpa.mapping.Map.<init>(Map.java:710)
        at ie.globetech.transitconnex.activity.bustrips.BusTripFragment.onEngineInitializationCompleted(BusTripFragment.java:179)
        at com.here.android.mpa.common.MapEngine.init(MapEngine.java:279)
        at ie.globetech.transitconnex.activity.bustrips.BusTripFragment.onCreate(BusTripFragment.java:136)
        at android.support.v4.app.Fragment.performCreate(Fragment.java:2331)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1386)
        at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1188)
        at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1071)
        at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:115)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2380)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
        at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3248)
        at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3200)
        at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:195)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597)
        at ie.globetech.transitconnex.activity.bustrips.BusTripMainActivity.onStart(BusTripMainActivity.java:125)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1256)
        at android.app.Activity.performStart(Activity.java:6965)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2934)
            ... 9 more

片段布局:

<com.here.android.mpa.mapping.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true" />

片段类:

    public class BusTripFragment extends Fragment{

           private Activity mActivity;
           private Map map = null;
           private MapView mapView;

             @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    ApplicationContext appContext = new ApplicationContext(mActivity.getApplicationContext());
                    MapEngine.getInstance().init(appContext, this);

              }

 @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mActivity = (Activity) activity;
            //mCallbacks = (MyNavigationDrawerCallbacks) activity;
        } catch (ClassCastException e) {}
    }


        @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View view = inflater.inflate(R.layout.trip_fragment, container, false);
               mapView = (MapView) view.findViewById(R.id.map);
               return view;      
                }

    @Override
        public void onEngineInitializationCompleted(Error error) {

            // retrieve a reference of the map from the map fragment
            //map = mapFragment.getMap();

            isMapEngineInitialized = true;

            if (map == null) {
                map = new Map();
            }

            mapView.setMap(map);

            // Set the map center to the Vancouver region (no animation)
            map.setCenter(new GeoCoordinate(49.196261, -123.004773, 0.0), Map.Animation.NONE);

            // Set the zoom level to the average between min and max
            map.setZoomLevel((map.getMaxZoomLevel() + map.getMinZoomLevel()) / 2);
        }

     }

我使用的是HERE SDK版本:3.7.0.118

我想通过下面的therads

  1. HERE Map/MapView without using MapFragment
  2. 任何人都知道如何在Fragment中处理Here Maps MapView?

2 个答案:

答案 0 :(得分:1)

我自己的解决方案,它运作良好。完全删除了 MapView

刚刚在片段

的布局xml中添加了FrameLayout
 <FrameLayout
        android:id="@+id/simpleFrameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true" /> 

现在,在 onCreateView(...)中动态创建 MapFragment 并添加到 FrameLayout 和init

    MapFragment mapFragment = new MapFragment();
    FragmentManager fm = mActivity.getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.simpleFrameLayout, mapFragment).commit();

    mapFragment.init(mActivity, new OnEngineInitListener() {
                @Override
                public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
                    if (error == OnEngineInitListener.Error.NONE) {

                        // retrieve a reference of the map from the map fragment
                        map = mapFragment.getMap();
                    } else {
                        logger.error("ERROR: Cannot initialize Map Fragment: " + error.getStackTrace());
                    }
                }
            });

答案 1 :(得分:0)

根据堆栈跟踪: 引起:com.here.android.mpa.common.UnintializedMapEngineException:在MapEngine初始化之前无法创建HERE SDK对象。见MapEngine.init()

从您的源代码: public void onEngineInitializationCompleted(错误错误){

        // retrieve a reference of the map from the map fragment
        //map = mapFragment.getMap();

        isMapEngineInitialized = true;

... }

您没有在onEngineInitializationCompleted中检查错误返回码。

您看到的调用堆栈意味着从引擎设置返回错误。请打印错误,我们可以找到合适的解决方案。