Java 谷歌地图片段不';t载荷

Java 谷歌地图片段不';t载荷,java,android,google-maps,android-fragments,Java,Android,Google Maps,Android Fragments,我想加载一个对象并创建一些侦听器: 我创建了这个类: 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

我想加载一个对象并创建一些侦听器:

我创建了这个类:

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文件中的第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" />

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

在主类中,使用:

map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
而不是:

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

要将
SupportMapFragment
添加到项目中,您需要添加
google-support-v4
库和
google地图库
。你可以在我写的一篇博文中读到如何做到这一点。

FragmentActivity
由Android支持库v4提供,以允许Android平台(蜂窝)使用片段。对于Honeycom和更高版本,使用标准
活动
类支持片段

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

  • 使用
    android.support.v4.app.FragmentActivity
    ,您必须使用
    android.support.v4.app.Fragment
  • 使用
    android.app.Activity
    ,您必须使用
    android.app.Fragment
请注意包的差异

现在,关于Android版谷歌地图v2,您有两个片段实现:

  • MapFragment
    它扩展了蜂窝和更高版本的
    android.app.Fragment
  • SupportMapFragment
    扩展了
    android.support.v4.app.Fragment
    以支持旧版本的android

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

尝试使用,class=“com.google.android.gms.maps.SupportMapFragment”。我试过了,现在它在第行显示nullPointerException:map=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();是一个很好的一步一步实现GoogleMapV2的教程。希望这能对你有所帮助。让谷歌地图在安卓系统上运行的难度是惊人的。如果有人想知道Android作为一个开发环境有多糟糕,我会指向Maps。谢谢,它向我显示了一个错误:“SupportMapFragment无法解析为类型”你导入了所有必需的包吗?嗨,我想我添加了所有必需的导入,但是我使用的所有导入都写在主线程中,请让我知道如果有什么遗漏,您需要添加google-play-services.jar来构建path,并将google-play-services_lib作为库添加到您的项目中。检查@Emil Adz的link.hi,我已经将google-play-services_lib作为库导入到我的项目中,然后检查您的导入,您是否导入了正确的类。我导入的内容与您在此处编写的教程中的内容相同,所以我不确定,为了使用SupportMapFragment,我需要编写哪些导入内容?请看我在主线中为凡人所作的介绍。
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();