【发布时间】:2017-06-29 03:43:10
【问题描述】:
我的应用程序有 3 个选项卡,我想在其中一个选项卡中显示地图(HERE MAPS,而不是 Google 地图)。我能够让地图在 Activity 中完美运行,但是在 Fragment 类中,当我尝试将 Fragment 视图投射到 MapFragment 时会引发错误。
MapFragment mapFragment = (MapFragment)
getFragmentManager().findFragmentById(R.id.mapfragment);
错误: 不可转换的类型:无法将 android.support.v4.app.support 转换为 com.here.android.mpa.mapping.MapFragment
如果我错了,请纠正我,但发生这种情况的原因是因为我们不能在 android.support.v4 中使用 android.app.Fragment(用于显示地图,如 HERE MAPS DOC 中的说明) .app.Fragment(用于TabsLayout)。
我使用 Google 地图发现了许多与此错误相关的问题。但是只有两个(first,second)在使用HERE MAPS时出现了相同的错误,并且没有一个真正有助于解决问题。
在谷歌地图中,您可以使用 SupportMapFragment(),但此方法仅适用于谷歌地图。使用Here Maps有什么解决方案吗?也许用不同的方法来达到相同的目标?还是我在 TabLayout 中实现此 Here Maps 时遗漏了什么?
任何帮助将不胜感激!
我的代码:
MapFragment.java
public class Map extends Fragment {
public Map() {}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
MapFragment mapFragment = (MapFragment)
getFragmentManager().findFragmentById(R.id.mapfragment);
mapFragment.init(new OnEngineInitListener() {
@Override
public void onEngineInitializationCompleted(
OnEngineInitListener.Error error) {
if (error == OnEngineInitListener.Error.NONE) {
com.here.android.mpa.mapping.Map map = mapFragment.getMap();
} else {
System.out.println("ERROR: Cannot initialize MapFragment");
}
}
};
return view;
}
fragment_map.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.modulos.tabsMenu.Config">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/mapFragmentContainer"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
android:background="#aaa" >
<fragment
class="com.here.android.mpa.mapping.MapFragment"
android:id="@+id/mapfragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
android:background="#aaa" >
<!-- other things here -->
</LinearLayout>
</FrameLayout>
【问题讨论】:
标签: android android-fragments here-api