如何在Fragment中创建MapView

时间:2012-08-09 10:39:03

标签: android exception android-fragments android-mapview

我收到了这个错误:

android.view.InflateException: Binary XML file line #13: Error inflating class com.google.android.maps.MapView

这是我的MapView类

public class MapViewActivity extends BaseFragment {
private TextView mTvTitle;
private View mView;

/*mapview members*/
private MapView mMapView;
private MapController mapController;
private LocationManager locationManager;
private MyLocationOverlay myLocationOverlay;
private static final int DEFAULT_ZOOM = 14;
private GeoUpdateHandler locationListener =  new GeoUpdateHandler();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}


@Override
public void onResume() {
    super.onResume();
    myLocationOverlay.enableMyLocation();
    myLocationOverlay.enableCompass();
}

@Override
public void onPause() {
    super.onPause();
    locationManager.removeUpdates(locationListener);
    myLocationOverlay.disableMyLocation();
    myLocationOverlay.disableCompass();
}

@Override
public void onDestroy() {
    super.onDestroy();
    myLocationOverlay.disableMyLocation();
    myLocationOverlay.disableCompass();
}

@SuppressWarnings("unchecked")
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mView = inflater.inflate(R.layout.map_activity, container, false);
    mTvTitle  = (TextView) mView.findViewById(R.id.tvTitle);
    mTvTitle.setText(getResources().getString(R.string.top_tab_around));

    mMapView = (MapView) mView.findViewById(R.id.mvMapAroundMe);

    BindMap();
    return mView;
}

private void BindMap() {

    mMapView.preLoad();
    mMapView.setBuiltInZoomControls(true);
    mMapView.setSatellite(false);
    mapController = mMapView.getController();
    mapController.setZoom(14); // Zoom 1 is world view

    locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

    myLocationOverlay = new MyLocationOverlay(getActivity(), mMapView);
    mMapView.getOverlays().add(myLocationOverlay);

    Drawable drawable = this.getResources().getDrawable(R.drawable.pin);
    List<Overlay> mapOverlays = mMapView.getOverlays();
    MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable, getActivity());
    OverlayItem oItem = new OverlayItem(GeopointFactory(33.446886, -86.057426),  "title", "title2");
    itemizedoverlay.addOverlay(oItem);
    mapOverlays.add(itemizedoverlay);
    mapOverlays.add(itemizedoverlay);

    myLocationOverlay.runOnFirstFix(new Runnable() {
        public void run() {
            animateToPlaceOnMap ( GeopointFactory(33.446886, -86.057426) );
        }
    });
}


public class GeoUpdateHandler implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        mapController.animateTo( GeopointFactory(location.getLatitude(), location.getLongitude()));
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}


private class MyItemizedOverlay extends ItemizedOverlay {
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    Context mContext;

    public MyItemizedOverlay(Drawable marker) {
        super(boundCenterBottom(marker));
    }

    public MyItemizedOverlay(Drawable marker, Context context) {
        super(boundCenterBottom(marker));
        mContext = context;
    }

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

    @Override
    public int size() {
        return mOverlays.size();
    }

    @Override
    protected boolean onTap(int index) {
        final int tempIndex = index;
        OverlayItem item = mOverlays.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());

        dialog.setPositiveButton("פרטים",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        dialog.show();
        return true;
    }
}

private void animateToPlaceOnMap(final GeoPoint geopoint) {
    mMapView.post(new Runnable() {
        @Override
        public void run() {
            mMapView.invalidate();
            mapController.animateTo(geopoint);
            mapController.setZoom(DEFAULT_ZOOM);
        }
    });
}

private GeoPoint GeopointFactory(double latitude, double longitude) {
    int lat = (int) (latitude * 1E6);
    int lng = (int) (longitude * 1E6);
    return new GeoPoint(lat, lng);
}

}

这是我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="il.co.toeat" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.VIBRATE" />


<application android:label="@string/app_name" android:icon="@drawable/icon" android:name=".views.MainApplication">
    <uses-library android:name="com.google.android.maps" />

    <activity  android:name=".views.SplashActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar" android:noHistory="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".views.ViewportActivity"
              android:windowSoftInputMode="adjustPan|adjustResize"
              android:label="@string/app_name"
              android:configChanges="locale"
              android:screenOrientation="portrait"
              android:theme="@android:style/Theme.NoTitleBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>
</application>

1 个答案:

答案 0 :(得分:-1)

使用Google Map API无法以这种方式执行此操作。

从2012年12月起,您可以将MapFragment和SupportMapFragment与Google Map API v2一起使用,更多信息请点击此处:https://developers.google.com/maps/documentation/android/

修改

我觉得你不理解我。你不应该像你一样把MapView放在Fragment中。

通常不在简单片段中使用MapView(在新MapFragments之前)。 有一个允许它的黑客,你可以在这里阅读它https://stackoverflow.com/a/8126287/1233021 我认为当你不需要做任何开销时,最好使用新的MapFragments ......它们自12月12日起可用。该链接位于上一条消息中。

顺便说一句:出现问题但有其他错误https://stackoverflow.com/a/9448871/1233021