2

我目前正在开发新的 Google Maps Android API v2。

我的 MapActivity 类正在与我的三星 Galaxy s3 ans s2 一起使用,代码如下:

但是,有些特定设备无法正常工作,例如:GT-S5830i

class MapActivity extends FragmentActivity  implements OnMapClickListener, OnMapLongClickListener, OnMarkerClickListener
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

                tvLocInfo = (TextView)findViewById(R.id.locinfo);

                android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager();
                SupportMapFragment myMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
                myMap = myMapFragment.getMap();

                myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                ArrayList<Cooridnates> cooridnatesList = MainActivity.getList();
                for(int i=0;i<cooridnatesList.size();i++)
                {
                    //Toast.makeText(MapActivity.this, "SIZE : " + " " + cooridnatesList.size(), Toast.LENGTH_LONG).show();
                    LatLng point = new LatLng(cooridnatesList.get(i).getLat(),cooridnatesList.get(i).getLon());
                    tvLocInfo.setText("markers added");
                    myMap.addMarker(new MarkerOptions().position(point).title(point.toString()));
                    center=CameraUpdateFactory.newLatLng(point);
                }

                CameraUpdate zoom=CameraUpdateFactory.zoomTo(16);
                myMap.moveCamera(center);
                myMap.animateCamera(zoom);
                myMap.setOnMapClickListener(this);
                myMap.setOnMarkerClickListener(this);
                markerClicked = false;
             }

出于某种原因,我收到此错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.coldice.plotfinder/com.coldice.plotfinder.MapActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source)
at com.coldice.plotfinder.MapActivity.onCreate(MapActivity.java:70)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more
4

3 回答 3

4

您的函数MainActivity.getList()可能返回了一个空列表,因此 for 循环不会执行center=CameraUpdateFactory.newLatLng(point);

于 2013-01-27T19:59:58.863 回答
0

似乎中心null在这个 >>myMap.moveCamera(center);中,在将它传递给 moveCamera 方法之前对这个变量进行适当的检查。

于 2013-01-27T19:55:53.360 回答
0

似乎问题就在这里。center可能有空值,所以为了得到正确的解释调试你的应用程序。

center在这里初始化。

center=CameraUpdateFactory.newLatLng(point); 

它可能会在中心分配空值。

myMap.moveCamera(center);  <<<PROBLEM is here

if(center != null )
      myMap.moveCamera(center); 
于 2013-01-27T19:59:30.293 回答