android - 如何在 Fragment 中创建一个 MapView

标签 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);
}

这是我的 list :

<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>

最佳答案

您不能使用 Google Map API 以这种方式执行此操作。

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

编辑

我觉得你不了解我。您不应该按照您的方式将 MapView 放入 Fragment 中。

通常不会在简单的 Fragment 中使用 MapView(在新的 MapFragment 之前)。 有一个允许它的 hack,你可以在这里阅读 https://stackoverflow.com/a/8126287/1233021 但是我认为当您不需要做任何开销时最好使用新的 MapFragments...它们从 12 月 12 日开始可用。该链接在上一条消息中。

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

关于android - 如何在 Fragment 中创建一个 MapView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11881559/

相关文章:

android - Recycler View 在滚动时随机播放图像

android - 打盹模式 - 前台服务是否继续运行?

haskell - 在 Haskell 中执行一系列操作时的异常处理

java - java中catch与catch并重新抛出异常之间的区别?

android - 在 fragment 中引用 SupportFragmentManager

android - 在 Fragment 中哪里发起 View ? onViewCreated 或 onActivityCreated

android - Android 的 X.509 认证

android - Dalvik 文件格式 (*.dx) 是否支持比 Java .class 文件更多的指令?

java - Java 中的 printStackTrace 与 Logger 框架

android - 如何修复 FragmentManager 已经在 FragmentTransaction 中执行事务?