getFragmentManager时的GoogleMap FATAL EXCEPTION

时间:2013-01-19 18:13:27

标签: google-maps android-fragmentactivity google-maps-android-api-2

我有一个完美的地图活动,但当我添加标记时,我收到错误,但没有找到解决问题的方法。

  

公共类MapActiv扩展了FragmentActivity {

//private GoogleMap map;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_fragment);
    GoogleMap mGMap = ((MapFragment) 
            getFragmentManager().findFragmentById(R.id.map)).getMap();
    mGMap.addMarker(new MarkerOptions().position(new LatLng(46.24801, 26.76173))
    .title("Title").draggable(false));
}

}

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"
    map:cameraTargetLat="46.24801"
    map:cameraTargetLng="26.76173"
    map:cameraTilt="30"
    map:cameraZoom="18"
    map:uiCompass="false"
    map:uiRotateGestures="true"
    map:uiScrollGestures="true"
    map:uiTiltGestures="true"
    map:uiZoomControls="false"
    map:uiZoomGestures="true"/>
  

01-19 19:48:15.805:E / AndroidRuntime(16616):致命异常:主01-19   19:48:15.805:E / AndroidRuntime(16616):java.lang.NoSuchMethodError:com.ex.myschoolapp.MapActiv.getFragmentManager   01-19 19:48:15.805:E / AndroidRuntime(16616):at com.ex.myschoolapp.MapActiv.onCreate(MapActiv.java:23)01-19   19:48:15.805:E / AndroidRuntime(16616):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)   01-19 19:48:15.805:E / AndroidRuntime(16616):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)   01-19 19:48:15.805:E / AndroidRuntime(16616):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)   01-19 19:48:15.805:E / AndroidRuntime(16616):在android.app.ActivityThread.access $ 1500(ActivityThread.java:121)01-19   19:48:15.805:E / AndroidRuntime(16616):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:943)   01-19 19:48:15.805:E / AndroidRuntime(16616):在android.os.Handler.dispatchMessage(Handler.java:99)01-19   19:48:15.805:E / AndroidRuntime(16616):在android.os.Looper.loop(Looper.java:123)   01-19 19:48:15.805:E / AndroidRuntime(16616):在android.app.ActivityThread.main(ActivityThread.java:3701)01-19   19:48:15.805:E / AndroidRuntime(16616):at java.lang.reflect.Method.invokeNative(Native Method)01-19   19:48:15.805:E / AndroidRuntime(16616):at java.lang.reflect.Method.invoke(Method.java:507)   01-19 19:48:15.805:E / AndroidRuntime(16616):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:862)   01-19 19:48:15.805:E / AndroidRuntime(16616):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)01-19   19:48:15.805:E / AndroidRuntime(16616):at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

试试这个(请记住,如果您使用的是版本&lt; 3.0,则必须使用android支持库)

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;

    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
    import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
    import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
    import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.Marker;
    import com.google.android.gms.maps.model.MarkerOptions;
    public class MainActivity extends android.support.v4.app.FragmentActivity implements OnMapClickListener, OnMapLongClickListener{
        private GoogleMap myMap;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            FragmentManager myFragmentManager = getSupportFragmentManager();
            SupportMapFragment myMapFragment 
               = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
              myMap = myMapFragment.getMap();
              myMap.setOnMapLongClickListener(this);
              myMap.setOnMapClickListener(this);
              myMap.setInfoWindowAdapter(new MiInfoWindowAdapter());
              myMap.setOnMarkerClickListener(new OnMarkerClickListener() {

                @Override
                public boolean onMarkerClick(Marker marker) {
                    // TODO Auto-generated method stub
                    marker.hideInfoWindow();
                    return false;
                }
            });

        }

         @Override
         public void onMapLongClick(LatLng point) {

             myMap.addMarker(new MarkerOptions().position(point).title(point.toString()));
         }

        @Override
        public void onMapClick(LatLng point) {
            // TODO Auto-generated method stub
            myMap.addMarker(new MarkerOptions().position(point).title(point.toString()));

        }

        public class MiInfoWindowAdapter implements InfoWindowAdapter{

            @Override
            public View getInfoContents(Marker marker) {
                LayoutInflater inflater = getLayoutInflater().from(getApplicationContext());
                View view = inflater.inflate(R.layout.info, null);
                marker.setDraggable(true);
                return view;
            }

            @Override
            public View getInfoWindow(Marker marker) {
                LayoutInflater inflater = getLayoutInflater().from(getApplicationContext());
                View view = inflater.inflate(R.layout.info, null);
                marker.setDraggable(true);
                return view;
            }

        }
    }

答案 1 :(得分:0)

当应用程序使用外部库时,可能会抛出NoSuchMethodError错误。假设您的应用程序是使用特定版本的外部库编译和执行的。在某些时候,外部库已更改,某些方法已被删除或更新。如果未更新应用程序的类路径并且未使用最新版本的外部库编译代码,则在运行时期间将调用不再存在的方法,并且将抛出NoSuchMethodError错误。

所以我更新了Google Map API

来自:com.google.android.gms:play-services-maps:7.5.0

收件人:com.google.android.gms:play-services-maps:7.8.0