android – 在framelayout中访问动态添加的谷歌地图片段

android – 在framelayout中访问动态添加的谷歌地图片段,第1张

概述我在framelayout中动态添加了我的谷歌地图片段.现在我想把标记和谷歌地图做其他的东西.但我无法通过get片段管理器访问它.代码显示没有错误但是当它执行时它返回空指针异常. 我认为它的逻辑错误.与类型铸造有关.请帮助 这是我的“nearby_places.xml”布局 <?xml version="1.0" encoding="utf-8"?><RelativeLayout xm 我在framelayout中动态添加了我的谷歌地图片段.现在我想把标记和谷歌地图做其他的东西.但我无法通过get片段管理器访问它.代码显示没有错误但是当它执行时它返回空指针异常.

我认为它的逻辑错误.与类型铸造有关.请帮助

这是我的“nearby_places.xml”布局

<?xml version="1.0" enCoding="utf-8"?><relativeLayout     xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:ID="@+ID/map_area"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    >    <com.localiteproximus.CustomautoCompleteTextVIEw    androID:ID="@+ID/atv_places"    androID:layout_wIDth="fill_parent"    androID:layout_height="wrap_content"            androID:layout_alignParenttop="true"    androID:hint="@string/str_atv_places"    androID:singleline="true" />    <FrameLayout    androID:ID="@+ID/mapVIEw"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"         androID:layout_below="@ID/atv_places"></FrameLayout></relativeLayout>

这是我的班级

public class NearByPlaceFragment extends Fragment {public NearByPlaceFragment(){}VIEw rootVIEw;GoogleMap GoogleMap;public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) {        MapFragment fragment = new MapFragment();        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();        transaction.add(R.ID.mapVIEw,fragment).commit(); rootVIEw = inflater.inflate(R.layout.nearby_places,container,false);  GoogleMap = ((MapFragment) getFragmentManager().findFragmentByID(                    R.ID.map)).getMap(); return rootVIEw}

和LogCat显示这一点

58.608: E/AndroIDRuntime(12654): FATAL EXCEPTION: main03-22 03:00:58.608: E/AndroIDRuntime(12654): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.localiteproximus/com.localiteproximus.NearbyPlaces}: androID.vIEw.InflateException: Binary XML file line #21: Class is not a VIEw com.Google.androID.gms.maps.MapFragment03-22 03:00:58.608: E/AndroIDRuntime(12654):    at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2294)03-22 03:00:58.608: E/AndroIDRuntime(12654): Caused by: java.lang.classCastException: com.Google.androID.gms.maps.MapFragment cannot be cast to androID.vIEw.VIEw03-22 03:00:58.608: E/AndroIDRuntime(12654):    at java.lang.class.asSubclass(Class.java:1182)03-22 03:00:58.608: E/AndroIDRuntime(12654):    at androID.vIEw.LayoutInflater.createVIEw(LayoutInflater.java:565)03-22 03:00:58.608: E/AndroIDRuntime(12654):    ... 23 more
解决方法 我自己得到了答案.

所以这真是一个愚蠢的错误.6小时后我发现错误就在这条线上

GoogleMap = ((MapFragment) getFragmentManager().findFragmentByID(R.ID.mapVIEw)).getMap();

它正在获取其父视图而不是子视图,因此将其替换为此行

GoogleMap = ((MapFragment) getChildFragmentManager().findFragmentByID(R.ID.mapVIEw)).getMap();

一切都很好:).

总结

以上是内存溢出为你收集整理的android – 在framelayout中访问动态添加的谷歌地图片段全部内容,希望文章能够帮你解决android – 在framelayout中访问动态添加的谷歌地图片段所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1129422.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存