6

我想加载一个谷歌地图对象并创建一些监听器:

我创建了这个类:

import java.lang.ref.WeakReference;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
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.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.Toast;

public class itemSaleActivity extends FragmentActivity  {
    private static Context app;
    private static GoogleMap map;
    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
    private static final int LOAD_COORD = 0;
    private ImageView pic;
    private LocationHandler mHandler;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // Set View to register.xml
        setContentView(R.layout.itemsale);

        app = getApplicationContext();
        Bundle extras = getIntent().getExtras();
        byte[] byteArray = extras.getByteArray("picture");
        Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

        pic = (ImageView) findViewById(R.id.itemImage);
        pic.setImageBitmap(bmp);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        //Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG).title("Hamburg"));
        mHandler = new LocationHandler(this);

        OnMarkerDragListener markerDragListener = new OnMarkerDragListener() {

            @Override
            public void onMarkerDragStart(Marker marker) {
                // Called when the marker drag is started
            }

            @Override
            public void onMarkerDragEnd(Marker marker) {
                // Called when the marker is dropped down.
                double[] coords = null;
                coords[0] = marker.getPosition().latitude;
                coords[1] = marker.getPosition().longitude;
                RestoreUIwithSavedLocation(coords);
                Toast.makeText(getApplicationContext(),"Pin Dropped at: " + coords[0] + ", " + coords[1]+marker.getTitle() , Toast.LENGTH_LONG).show();
            }

            @Override
            public void onMarkerDrag(Marker marker) {

            }
        };

        map.setOnMarkerDragListener(markerDragListener);

        View titleView = getWindow().findViewById(android.R.id.title);
        if (titleView != null) {
            ViewParent parent = titleView.getParent();
            if (parent != null && (parent instanceof View)) {
             View parentView = (View)parent;
             parentView.setVisibility(View.GONE);
            }
        }

        // You can also assign the title programmatically by passing a
        // CharSequence or resource id.
    }

    private void RestoreUIwithSavedLocation(double[] coordsArray) {
        Message.obtain(mHandler, LOAD_COORD, coordsArray).sendToTarget();
    }

    static class LocationHandler extends Handler {
        WeakReference<Activity> mActivity;

        public LocationHandler(Activity activity) {
            mActivity = new WeakReference<Activity>(activity);
        }

        public void handleMessage(Message msg) {
            Activity contextActivity = mActivity.get();
            switch ((int)msg.what) {

            case LOAD_COORD:
                map.clear();
                double[] coordArray = (double[])msg.obj;
                Marker marker = map.addMarker(new MarkerOptions().position(new LatLng(coordArray[0], coordArray[1])));
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(coordArray[0], coordArray[1]), 18));
                map.animateCamera(CameraUpdateFactory.zoomTo(18), 2000, null);
                marker.setDraggable(true);
                String s = Double.toString(coordArray[0]) + ", " + Double.toString(coordArray[1]);
                Toast.makeText(app,"in the case"+s , Toast.LENGTH_LONG).show();
                break;
            }
        }
    }
}

这是 XML:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/itemImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/logo" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="400dp"
        android:layout_marginTop="10dp"
        android:layout_weight="66496.79"
        android:orientation="horizontal" >

        <fragment
            android:id="@+id/map"
            class="com.google.android.gms.maps.MapFragment"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginRight="150dp" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="170dp"
            android:orientation="vertical" >

            <Button
            android:id="@+id/messageBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:text="fffff" />

            <Button
                android:id="@+id/call"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:text="ffff" />

        <Button
            android:id="@+id/sms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/button1"
            android:layout_centerVertical="true"
            android:text="ffff" />
        <Button
            android:id="@+id/email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/button1"
            android:layout_centerVertical="true"
            android:text="fffff" />

        <Button
            android:id="@+id/navigate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button1"
            android:layout_alignParentEnd="false"
            android:layout_alignParentStart="false"
            android:layout_below="@+id/button1"
            android:layout_marginTop="14dp"
            android:text="ffffffffff" />
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

当我在手机(galaxy s2)上调试它时,它向我显示了一个异常:

03-29 15:24:29.750: E/AndroidRuntime(2579): FATAL EXCEPTION: main
03-29 15:24:29.750: E/AndroidRuntime(2579): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sal/com.example.sal.itemSaleActivity}: android.view.InflateException: Binary XML file line #26: Error inflating class fragment
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.app.ActivityThread.access$600(ActivityThread.java:132)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.os.Looper.loop(Looper.java:137)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.app.ActivityThread.main(ActivityThread.java:4575)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at java.lang.reflect.Method.invokeNative(Native Method)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at java.lang.reflect.Method.invoke(Method.java:511)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at dalvik.system.NativeStart.main(Native Method)
03-29 15:24:29.750: E/AndroidRuntime(2579): Caused by: android.view.InflateException: Binary XML file line #26: Error inflating class fragment
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.app.Activity.setContentView(Activity.java:1835)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at com.example.sal.itemSaleActivity.onCreate(itemSaleActivity.java:45)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.app.Activity.performCreate(Activity.java:4465)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
03-29 15:24:29.750: E/AndroidRuntime(2579):     ... 11 more
03-29 15:24:29.750: E/AndroidRuntime(2579): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.support.v4.app.Fragment.instantiate(Fragment.java:388)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.support.v4.app.Fragment.instantiate(Fragment.java:363)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264)
03-29 15:24:29.750: E/AndroidRuntime(2579):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
03-29 15:24:29.750: E/AndroidRuntime(2579):     ... 22 more

其中 xml 文件中的第 26 行是:

  <fragment
                android:id="@+id/map"
                class="com.google.android.gms.maps.MapFragment"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginRight="150dp" />
4

3 回答 3

10

在 XML 文件中,使用class="com.google.android.gms.maps.SupportMapFragment".

在你的主要课程中,使用:

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

代替:

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
于 2013-03-29T12:41:30.570 回答
2

要添加SupportMapFragment到您的项目中,您需要添加google-support-v4库和google map libraries. 您可以在我写的博客文章Google Maps API V2中了解如何做到这一点。

于 2013-03-29T12:44:56.113 回答
1

FragmentActivityAndroid Support Library v4提供,以允许Android 3 (Honeycomb) 之前的 Android 平台使用 Fragments。Activity对于 Honeycomb 及更高版本,标准类支持使用片段

问题是,根据您选择的活动类,您不能使用相同的片段类:

  • android.support.v4.app.FragmentActivity你可以使用android.support.v4.app.Fragment
  • android.app.Activity你必须使用android.app.Fragment

注意包装差异。

现在,对于 Android 版 Google Maps v2,您有两个 Fragment 实现:

  • MapFragment扩展android.app.Fragment到 Honeycomb 及更高版本
  • SupportMapFragment扩展android.support.v4.app.Fragment以支持较旧的 Android 版本

因此,当您使用FragmentActivity时,您必须SupportMapFragment为您的应用程序使用该实现。

于 2013-03-29T12:47:47.587 回答