1

我一直在尝试在地图上添加标记。目前,我有一个从另一个活动引用的谷歌地图,我正在尝试从当前活动访问同一个地图并且我正在处理它。但是我收到一个错误。

com.example.googlemapsV2;

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.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

/**
 * Adds a marker to the map with co-ordinates shown
 * 
 * @author Aaron
 * 
 */

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class mapAddingMarker extends FragmentActivity {

    private GoogleMap map;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
System.out.print("hello this works");
//       get map from base class
        currentLocation x = new currentLocation();
        x.onCreate(savedInstanceState);
        map = x.getMap(); // get the map reference

//map = ((MapFragment) getFragmentManager()
//      .findFragmentById(R.id.map)).getMap();
//addingMarkerToMap();
    }

    /**
     * Adds a marker to the current map with co-ordinates of location
     */

    public void addingMarkerToMap() {
        // make map clickable
                map.setOnMapClickListener(new OnMapClickListener() {//janmarstame

                    @Override
                    public void onMapClick(LatLng point) {

                        // creating marker options
                        MarkerOptions markerOp = new MarkerOptions();
                        // set position of marker at that point
                        markerOp.position(point);
                        // setting the title for the marker
                        markerOp.title(point.latitude + " : " + point.longitude);

                        // clear the previously touched position
                        map.clear();

                        // Animates the movement of the camera from the current position
                        // to the position defined in the update.
                        map.animateCamera(CameraUpdateFactory.newLatLng(point));
                        //place the marker on the map
                        map.addMarker(markerOp);

                    }

                });

    }

}

我的布局: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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

       <TextView
        android:id="@+id/tv_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

       <fragment
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"
          android:enabled = "true"
          android:clickable = "true"/>

</RelativeLayout>

我得到的错误

10-12 11:18:30.079: D/AbsListView(13426): Get MotionRecognitionManager
10-12 11:18:30.194: D/libEGL(13426): loaded /system/lib/egl/libEGL_mali.so
10-12 11:18:30.224: D/libEGL(13426): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-12 11:18:30.229: D/libEGL(13426): loaded /system/lib/egl/libGLESv2_mali.so
10-12 11:18:30.234: D/(13426): Device driver API match
10-12 11:18:30.234: D/(13426): Device driver API version: 10
10-12 11:18:30.234: D/(13426): User space API version: 10 
10-12 11:18:30.234: D/(13426): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
10-12 11:18:30.294: D/OpenGLRenderer(13426): Enabling debug mode 0
10-12 11:18:32.089: D/AndroidRuntime(13426): Shutting down VM
10-12 11:18:32.089: W/dalvikvm(13426): threadid=1: thread exiting with uncaught exception (group=0x41d152a0)
10-12 11:18:32.259: E/AndroidRuntime(13426): FATAL EXCEPTION: main
10-12 11:18:32.259: E/AndroidRuntime(13426): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlemapsV2/com.example.googlemapsV2.mapAddingMarker}: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.os.Looper.loop(Looper.java:137)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.app.ActivityThread.main(ActivityThread.java:4898)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at java.lang.reflect.Method.invokeNative(Native Method)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at java.lang.reflect.Method.invoke(Method.java:511)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at dalvik.system.NativeStart.main(Native Method)
10-12 11:18:32.259: E/AndroidRuntime(13426): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.app.Activity.setContentView(Activity.java:1924)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at com.example.googlemapsV2.mapAddingMarker.onCreate(mapAddingMarker.java:34)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.app.Activity.performCreate(Activity.java:5206)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
10-12 11:18:32.259: E/AndroidRuntime(13426):    ... 11 more
10-12 11:18:32.259: E/AndroidRuntime(13426): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.support.v4.app.Fragmenenter code heret.instantiate(Fragment.java:402)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)
10-12 11:18:32.259: E/AndroidRuntime(13426):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
10-12 11:18:32.259: E/AndroidRuntime(13426):    ... 21 more
4

1 回答 1

0

看来您错过了从中导入项目的机会

\extras\google\google_play_services\libproject\google-play-services_lib.

将其作为库添加到您当前的项目中。

确保您已将 SupportMapFragment 用于 11 以下的 API。

?xml version="1.0" encoding="utf-8"?>
  <fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
   android:name="com.google.android.gms.maps.SupportMapFragment"
   android:layout_width="wrap_content"
   android:layout_height="match_parent" />
于 2013-10-12T10:46:20.120 回答