5

我在fragment. 下面是我的代码。

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.tfick.android.R;

public class PoiFragment extends Fragment{

private GoogleMap mGoogleMap;

private static final LatLng SYDNEY = new LatLng(23.03,72.62);

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v=inflater.inflate(R.layout.poi_fragment, container,false);


    MapFragment  mMapFragment = MapFragment.newInstance();
     FragmentTransaction fragmentTransaction =
             getFragmentManager().beginTransaction();
     fragmentTransaction.add(R.id.container1, mMapFragment);
     fragmentTransaction.commit();

     if(mGoogleMap ==null){

            /*initializing map */

            mGoogleMap=mMapFragment.getMap();

            /*cretaing custom camera position*/

            CameraPosition mCameraPosition=new CameraPosition.Builder().target(SYDNEY).bearing(0).tilt(30).build();

            /*setting custom camera position*/

            mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(mCameraPosition));

            /*adding custom marker on the map.*/
             mGoogleMap.addMarker(new MarkerOptions().draggable(true).position(new LatLng(22.03, 72.56)).title("hello world").snippet("Ahmedabad").icon(BitmapDescriptorFactory.fromResource(R.drawable.wifi)));

            mGoogleMap.addMarker(new MarkerOptions().draggable(true).position(new LatLng(23.03, 78.10)).title("hello world").snippet("Baroda").icon(BitmapDescriptorFactory.fromResource(R.drawable.wifi)));

        }

        if(mGoogleMap !=null){


            /* map is already initialized then setting map type */
            /*
             * normal 
             * satellite
             * hybrid
             * terrain
             * none
             */
            mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        }

    return v;
}
}
03-01 12:15:31.439: E/AndroidRuntime(9299): FATAL EXCEPTION: main
03-01 12:15:31.439: E/AndroidRuntime(9299): java.lang.RuntimeException: Unable to start activity    ComponentInfo{com.tfick.android/com.tfick.android.PointOfIntrest}: java.lang.NullPointerException: CameraUpdateFactory is not initialized
03-01 12:15:31.439: E/AndroidRuntime(9299):     at    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at android.os.Looper.loop(Looper.java:137)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at  android.app.ActivityThread.main(ActivityThread.java:4898)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at java.lang.reflect.Method.invokeNative(Native Method)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at java.lang.reflect.Method.invoke(Method.java:511)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at dalvik.system.NativeStart.main(Native Method)
03-01 12:15:31.439: E/AndroidRuntime(9299): Caused by: java.lang.NullPointerException: CameraUpdateFactory is not initialized
03-01 12:15:31.439: E/AndroidRuntime(9299):     at com.google.android.gms.internal.at.a(Unknown Source)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at com.google.android.gms.maps.CameraUpdateFactory.J(Unknown Source)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at com.google.android.gms.maps.CameraUpdateFactory.newCameraPosition(Unknown Source)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at com.tfick.fragment.PoiFragment.onCreateView(PoiFragment.java:51)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at android.app.BackStackRecord.run(BackStackRecord.java:635)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at android.app.Activity.performStart(Activity.java:5215)
03-01 12:15:31.439: E/AndroidRuntime(9299):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
03-01 12:15:31.439: E/AndroidRuntime(9299):     ... 11 more
4

5 回答 5

1

多亏了我,如果有人遇到同样的问题,我找到了解决方案,请使用以下代码。

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.tfick.android.R;

public class PoiFragment extends MapFragment {
    /*object of google map*/
    public GoogleMap mGoogleMap;
    /* summy location*/
    private static final LatLng SYDNEY = new LatLng(23.03, 72.62);


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        if (getMap() != null) {
            Log.d(getClass().getSimpleName(), "Map ready for use!");

            mGoogleMap = getMap();

            /* cretaing custom camera position */

            CameraPosition mCameraPosition = new CameraPosition.Builder()
                    .target(SYDNEY).bearing(0).tilt(30).build();
            Log.i("gmap", mGoogleMap.toString());

            /* setting custom camera position */

            mGoogleMap.animateCamera(CameraUpdateFactory
                    .newCameraPosition(mCameraPosition));

            /* adding custom marker on the map. */
            mGoogleMap
                    .addMarker(new MarkerOptions()
                            .draggable(true)
                            .position(new LatLng(22.03, 72.56))
                            .title("hello world")
                            .snippet("Ahmedabad")
                            .icon(BitmapDescriptorFactory
                                    .fromResource(R.drawable.wifi)));

            mGoogleMap
                    .addMarker(new MarkerOptions()
                            .draggable(true)
                            .position(new LatLng(23.03, 78.10))
                            .title("hello world")
                            .snippet("Baroda")
                            .icon(BitmapDescriptorFactory
                                    .fromResource(R.drawable.wifi)));

        }

    }

}
于 2013-03-01T12:48:56.387 回答
0

我遇到了完全相同的问题。getMap()返回时会发生此错误null

我做了以下事情:

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

/* if googleMap == null then there may be following problem :
 * 1. google play service version are not matching on both device and installed apk.Will show update message on map.
 * 2. No google play service installed. Will show a install message on map.
 */
if (googleMap != null) {
    LatLng iniLoc = new LatLng(44.97651081283853, -93.2245754142059);
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(iniLoc));
    googleMap.getUiSettings().setZoomGesturesEnabled(false);
    currentMarkerOption = new MarkerOptions();
    currentMarkerOption.position(iniLoc);
    currentMarker = googleMap.addMarker(currentMarkerOption);
    currentMarker.setIcon((BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
    currentMarker.setTitle("Current Position");
    currentMarker.showInfoWindow();
    currentMarker.setVisible(false);
    initDestinationMarker();
}
于 2014-01-21T22:27:48.740 回答
0

问题是getMap()返回null并因此CameraUpdateFactory抛出异常,因为它需要一个初始化的GoogleMap.

我在使用MapFragmentinside a时遇到了类似的问题Fragment。我通过将地图初始化移动到OnResume.

于 2013-03-01T11:23:48.210 回答
0

Google 地图的实现很大程度上依赖于几个生命周期方法(onCreate、onCreateView、onDestroy、onResume 和 onPause)。如果将这些调用封装在自己的片段中,则应该将这些调用中继到 MapFragment。在您调用 mapView.getMap() 并获得 GoogleMap 作为回报后,地图应该已经初始化。

如果您从 getMap() 得到 null ,可能会发生几件事:

  • 您没有在 MapFragment 上调用 onCreate 和/或 onCreateView(在这些方法之后调用 getMap())
  • 您的清单中缺少 OpenGL ES 2 功能条目
  • 您缺少地图所需的权限
  • 设备上未安装 Google Play SDK 服务或它们太旧

我经常在 StackOverflow 上阅读有关 initialize(this) 解决方案的信息,但在大多数情况下实际上不需要它。

编辑: 我刚刚看到您正在通过事务添加 Mapfragment。您是否考虑过在 PoiFragment 中仅使用 MapView ?我相信通过使用 MapFragment 并通过事务添加它,您会遇到时间问题。当您仍在 PoiFragment.onCreateView 方法中时,事务可能尚未完成(因此 Maps 尚未初始化)。您可以尝试使用 onResume,但您必须确保只初始化一次地图代码,因为 onResume 会被更频繁地调用。

我在架构方面的建议:

  • 如果可行,直接在您的活动中使用 MapFragment
  • 如果您有足够的代码可以放在那里,请扩展 Mapfragment
于 2013-04-26T14:34:42.947 回答
0

初始化 CameraUpdateFactory。
根据文档 -
调用initialize(Context)。只要不抛出GooglePlayServicesNotAvailableException ,这个类就会被正确初始化。

链接- http://developer.android.com/reference/com/google/android/gms/maps/MapsInitializer.html#initialize(android.content.Context
)
如何初始化-

try {
     MapsInitializer.initialize(this /*the context*/);
 } catch (GooglePlayServicesNotAvailableException e) {
     e.printStackTrace();
 }

参考 - api v2 中的 mapview 和 cameraupdate

于 2013-03-01T07:34:48.833 回答