0

我的代码在下面

package com.example.googlemaps;

import android.app.Activity; import android.app.FragmentManager;
import android.graphics.Color; import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory; import
com.google.android.gms.maps.GoogleMap; import
com.google.android.gms.maps.MapFragment; import
com.google.android.gms.maps.model.BitmapDescriptorFactory; import
com.google.android.gms.maps.model.LatLng; import
com.google.android.gms.maps.model.MarkerOptions; import
com.google.android.gms.maps.model.Polygon; import
com.google.android.gms.maps.model.PolygonOptions;

public class MainActivity extends Activity{ //  static final LatLng
HAMBURG = new LatLng(53.558, 9.927); //  static final LatLng KIEL =
new LatLng(53.551, 9.993);   public GoogleMap map;

  @Override   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FragmentManager fr_mgr = getFragmentManager();
    MapFragment map_frag = (MapFragment)fr_mgr.findFragmentById(R.id.map);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap();

    map = map_frag.getMap();    map.setMyLocationEnabled(true);         LatLng Bhopal = new LatLng(23.233243200000000000,
77.434339400000000000);     //Move the camera instantly to hamburg with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(Bhopal, 15));

    // Zoom in, animating the camera.
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 
            map.addMarker(new MarkerOptions()   .position(new LatLng(23.233243200000000000,77.434339400000000000))  .title("Hello
Bhopal")
    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        Polygon polygon = map.addPolygon(new PolygonOptions()
    .add( new LatLng(23.226834,77.355309), new LatLng(23.214845,77.42672),
            new LatLng(23.187707,77.388954),    new LatLng(23.200961,77.31411))
    .strokeColor(Color.RED)
    .fillColor(Color.parseColor("#51000000")).strokeWidth(2));
  }
} 

XML

  <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=".MainActivity" >

       <fragment

        android:id="@+id/map"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout>

日志猫

08-27 12:52:47.753: E/Trace(1381): 打开跟踪文件时出错: 没有这样的文件或目录 (2) 08-27 12:52:47.793: E/dalvikvm(1381): 找不到类' com.google.android.gms.maps.MapFragment',引用自方法 com.example.googlemaps.MainActivity.onCreate 08-27 12:52:47.983: E/AndroidRuntime(1381): FATAL EXCEPTION: main 08-27 12: 52:47.983:E/AndroidRuntime(1381):java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.googlemaps/com.example.googlemaps.MainActivity}:android.view.InflateException:二进制 XML 文件第 7 行: 膨胀类片段时出错 08-27 12:52:47.983: E/AndroidRuntime(1381): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 08-27 12:52:47.983: E/AndroidRuntime(1381) ): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 08-27 12:52:47.983:E/AndroidRuntime(1381): 在 android.app.ActivityThread.access$600(ActivityThread.java:141) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 android.app.ActivityThread$H.handleMessage( ActivityThread.java:1234) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 android.os.Handler.dispatchMessage(Handler.java:99) 08-27 12:52:47.983: E/AndroidRuntime( 1381): 在 android.os.Looper.loop(Looper.java:137) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 android.app.ActivityThread.main(ActivityThread.java:5041) 08 -27 12:52:47.983: E/AndroidRuntime(1381): at java.lang.reflect.Method.invokeNative(Native Method) 08-27 12:52:47.983: E/AndroidRuntime(1381): at java.lang. reflect.Method.invoke(Method.java:511) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 08 -27 12:52:47。983: E/AndroidRuntime(1381): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 dalvik.system.NativeStart .main(Native Method) 08-27 12:52:47.983: E/AndroidRuntime(1381): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment 08-27 12:52:47.983 : E/AndroidRuntime(1381): 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 android.view.LayoutInflater.rInflate(LayoutInflater. java:746) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 android.view.LayoutInflater.inflate(LayoutInflater.java:489) 08-27 12:52:47.983: E/AndroidRuntime(1381) : 在 android.view.LayoutInflater.inflate(LayoutInflater.java:396) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 android.view。LayoutInflater.inflate(LayoutInflater.java:352) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 android.app.Activity.setContentView(Activity.java:1881) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 com.example.googlemaps .MainActivity.onCreate(MainActivity.java:26) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 android.app.Activity.performCreate(Activity.java:5104) 08-27 12:52:47.983 : E/AndroidRuntime(1381): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread. java:2144) 08-27 12:52:47.983: E/AndroidRuntime(1381): ... 11 更多 08-27 12:52:47.983: E/AndroidRuntime(1381):引起:android.app.Fragment$InstantiationException:无法实例化片段com.google.android.gms.maps.MapFragment:确保类名存在,是公共的,并且有一个公共的空构造函数 08-27 12:52 :47.983: E/AndroidRuntime(1381): 在 android.app.Fragment.instantiate(Fragment.java:592) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 android.app.Fragment.instantiate( Fragment.java:560) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 android.app.Activity.onCreateView(Activity.java:4709) 08-27 12:52:47.983: E/AndroidRuntime( 1381): 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) 08-27 12:52:47.983: E/AndroidRuntime(1381): ... 21 更多 08-27 12:52:47.983: E/ AndroidRuntime(1381):引起:java.lang.ClassNotFoundException:找不到类“com.google.android.gms.maps.MapFragment”在路径上:/data/app/com.example.googlemaps-2.apk 08-27 12:52:47.983: E/AndroidRuntime(1381): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65) 08- 27 12:52:47.983: E/AndroidRuntime(1381): 在 java.lang.ClassLoader.loadClass(ClassLoader.java:501) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 java.lang。 ClassLoader.loadClass(ClassLoader.java:461) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 android.app.Fragment.instantiate(Fragment.java:582) 08-27 12:52:47.983: E/AndroidRuntime(1381): ... 24 更多在 java.lang.ClassLoader.loadClass(ClassLoader.java:461) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 android.app.Fragment.instantiate(Fragment.java:582) 08-27 12 :52:47.983: E/AndroidRuntime(1381): ... 24 更多在 java.lang.ClassLoader.loadClass(ClassLoader.java:461) 08-27 12:52:47.983: E/AndroidRuntime(1381): 在 android.app.Fragment.instantiate(Fragment.java:582) 08-27 12 :52:47.983: E/AndroidRuntime(1381): ... 24 更多

4

1 回答 1

0
  public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {

        private GoogleMap mMap;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_dash);

                    SupportMapFragment mapFragment =(SupportMapFragment)getSupportFragmentManager()
                            .findFragmentById(R.id.map);
                    mapFragment.getMapAsync(MapActivity.this);


        }


        /**
         * Manipulates the map once available.
         * This callback is triggered when the map is ready to be used.
         * This is where we can add markers or lines, add listeners or move the camera. In this case,
         * we just add a marker near Sydney, Australia.
         * If Google Play services is not installed on the device, the user will be prompted to install
         * it inside the SupportMapFragment. This method will only be triggered once the user has
         * installed Google Play services and returned to the app.
         */
        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;

            // Add a marker in Sydney and move the camera
            final LatLng mapCenter = new LatLng(-35.2370, 149.0800);


            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(mapCenter)     // Sets the center of the map to Mountain View
                    .zoom(15)                   // Sets the zoom
                    .bearing(90)                // Sets the orientation of the camera to east
                    .tilt(40)                   // Sets the tilt of the camera to 30 degrees
                    .build();

            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));


            mMap.addMarker(new MarkerOptions()
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow_button))
                    .position(new LatLng(-35.234397096581276, 149.08692386001348))
                    .title("hello"));

    }

此代码可能会对您有所帮助。

于 2018-12-14T17:38:11.757 回答