Android:java.lang.NullPointerException:未初始化CameraUpdateFactory

时间:2013-06-19 03:20:15

标签: java android google-maps google-maps-android-api-2 google-play-services

我尝试实现自动右缩放级别以显示所有标记。但是,我得到Null Pointer异常。这是我的代码:

public class LocationMarkers extends FragmentActivity {
    private static GoogleMap supportMap;
    private ArrayList<LatLng> gp = new ArrayList<LatLng>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.location_marker);
        locationMarkers();

    }

    public void locationMarkers()
    {
        /*Implement Location Markers*/
        BufferedReader jsonReader = new BufferedReader(new InputStreamReader(this.getResources().openRawResource(R.raw.map)));
        StringBuilder jsonBuilder = new StringBuilder();
        try {
            for (String line = null; (line = jsonReader.readLine()) != null;) {
                jsonBuilder.append(line).append("\n");
            }

            JSONTokener tokener = new JSONTokener(jsonBuilder.toString());
            JSONArray jsonArray = new JSONArray(tokener);


            for (int index = 0; index < jsonArray.length(); index++) 
            {

                JSONObject jsonObject = jsonArray.getJSONObject(index);
                double getLat = jsonObject.getJSONObject("point").getDouble("lat");
                double getLng = jsonObject.getJSONObject("point").getDouble("long");
                String name   = jsonObject.getString("name");
                LatLng myLoc = new LatLng(getLat, getLng);
                gp.add(myLoc);
                Log.d("Long", Double.toString(getLng));             
                FragmentManager fmanager = getSupportFragmentManager();
                Fragment fragment = fmanager.findFragmentById(R.id.map);
                SupportMapFragment supportmapfragment = (SupportMapFragment)fragment;
                supportMap = supportmapfragment.getMap();
                    if(supportMap!=null){
                        Marker LocMarker = supportMap.addMarker(new MarkerOptions().position(myLoc)
                                  .title(name));

                        }

            }
              LatLngBounds.Builder builder = new LatLngBounds.Builder();
                for(LatLng m : gp) {
                    Log.e("Tag",Double.toString(m.latitude));
                    builder = builder.include(m);
                    }
                LatLngBounds bounds = builder.build();
                int padding = 0;
                CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 0);
                supportMap.moveCamera(cu);
        } catch (FileNotFoundException e) {
            Log.e("jsonFile", "file not found");
        } catch (IOException e) {
            Log.e("jsonFile", "ioerror");
        } catch (JSONException e) {
            Log.e("jsonFile", "error while parsing json");
        }

    }
}

我还得到另一个警告:Google Play services out of date. Requires 2012100 but found 1015我很高兴,如果你们帮忙找到问题?

1 个答案:

答案 0 :(得分:4)

1)希望您在不在模拟器中的设备上进行测试。您可能会在模拟器中出现错误,如果您仍想在模拟器上进行测试然后对其进行评论。它将在设备中完美地工作

2)您的Google Play服务已过期,因为您的Google Play服务库版本与您的模拟器Google Play服务版本不同。你必须安装更新的gms和自动贩卖apk文件。

克: http://uploaded.net/file/bnzl1si4

自动售货机: http://www.androidfilehost.com/?fid=9390135922294521859

希望它会对你有所帮助。