不幸的是,我在运行时地图中的地图视图中遇到问题

时间:2013-08-27 07:11:00

标签: android

我的代码在

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

Logcat

  

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):致命异常:主要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):在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):at和roid.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)08-27 12:52:47.983:E / AndroidRuntime(1381):at 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):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller .run(ZygoteInit.java:793)08-27 12:52:47.983:E / AndroidRuntime(1381):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)08-27 12:52 :47.983:E / AndroidRuntime(1381):at dalvik.system.NativeStart.main(Native Method)08-27 12:52:47.983:E / AndroidRuntime(1381):引起:android.view.InflateException:二进制XML文件行#7:错误膨胀类片段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):at 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):at android.view.LayoutInflater.inflate(LayoutInflater.java:352)08-27 12:52:47.983:E / AndroidRuntime(1381):at 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):at 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):at 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):at android.app.Activity.onCreateView(Activity .java:4709)08-27 12:52:47.983:E / AndroidRuntime(1381):在android.view.La youtInflater.createViewFromTag(LayoutInflater.java:680)08-27 12:52:47.983:E / AndroidRuntime(1381):... 21 more 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):at java.lang.ClassLoader.loadClass(ClassLoader.java) :501)08-27 12:52:47.983:E / AndroidRuntime(1381):at 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更多

1 个答案:

答案 0 :(得分: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"));

    }

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