谷歌地图片段

时间:2016-02-20 07:44:37

标签: java android

这是我的片段,我正在显示街景和Google地图,由于以下错误,我无法执行此操作:Screenshot of LogCat

FATAL EXCEPTION: main
Process: com.wi.guiddoo, PID: 9788
android.view.InflateException: Binary XML file line #12: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayouterInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.wi.guiddoo.fragments.RecommendationPagerSectionMap.onCreateView(RecommendationPagerSectionMap.java:48)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
at com.wi.guiddoo.utils.FragmentUtil.changeFragment(FragmentUtil.java:94)
at com.wi.guiddoo.fragments.RecommendationPagerSection$5.onClick(RecommendationPagerSection.java:274)

我尝试使用getChildFragmentManager以及getSupportFragmentManager,但我收到同样的错误。我还使用了SupportMapFragmentStreetViewPanoramaFragment并得到了相同的错误。

package com.wi.guiddoo.fragments;

public class RecomendationPagerSectionMap extends Fragment{
    double latitude,longitude;
    public String locationname;
    public View view;
    private GoogleMap googleMap;
    public TextView tv;
    public SupportStreetViewPanoramaFragment streetViewPanoramaFragment;

    public RecomendationPagerSectionMap(double Lat,double Long,String name){
        latitude=Lat;
        longitude=Long;
        locationname=name;
    }

     public RecomendationPagerSectionMap() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        view = inflater.inflate(R.layout.fragment_recomendation_pager_section_maps, container, false);
        AppController.getInstance().trackScreenView(locationname.toUpperCase(Locale.US));
        init();
        return view;
    }

    public void init(){
        tv=(TextView)view.findViewById(R.id.textview_streetview);
        streetViewPanoramaFragment = (SupportStreetViewPanoramaFragment) getChildFragmentManager().findFragmentById(R.id.streetviewfragment);
        streetViewPanoramaFragment.getStreetViewPanoramaAsync(new OnStreetViewPanoramaReadyCallback() {

            @Override
            public void onStreetViewPanoramaReady(final StreetViewPanorama streetview) {
                // TODO Auto-generated method stub
                streetview.setPosition(new LatLng(latitude, longitude),100);
            }
        });
        try {
            if (googleMap == null) {
                googleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
            }
            googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 17));
            googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title(locationname.toUpperCase(Locale.US)));
            //TP.setPosition(SYDNEY);
    }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我从这个片段调用它并且它无法正常工作:

 public class RecomendationPagerSection extends Fragment implements OnClickListener {...



  public void sendLatLongtoMap(final double lati,final double loni,final String name){ 
mapAndStreetView.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 
// TODO Auto-generated method stub 
    FragmentUtil.changeFragment(getActivity(),new RecomendationPagerSectionMap(lati,loni,name)); } }); }

2 个答案:

答案 0 :(得分:0)

根据您的跟踪,问题来自您的XML文件:

android.view.InflateException: Binary XML file line #12

检查您的R.layout.fragment_recomendation_pager_section_maps或任何相关的xml文件。

答案 1 :(得分:0)

试试这个

view = inflater.inflate(R.layout.fragment_recomendation_pager_section_maps,null);

并使您的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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Googlemap.Googlemap">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"/>

</RelativeLayout>

您使用supportfragment manager或childfragment manager

      googleMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();