Android Google Maps CameraUpdateFactory not initialized
Asked Answered
T

3

8

I want ro display a point as my location on the map, but when I set the camera on a map why this error occurs ?

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chinatown.wangjian.chinatown_nearbyme/com.chinatown.wangjian.chinatown_nearbyme.MainActivity}: java.lang.NullPointerException: CameraUpdateFactory is not initialized
                                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
                                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2099)
                                                                                           at android.app.ActivityThread.access$600(ActivityThread.java:134)
                                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                           at android.os.Looper.loop(Looper.java:137)
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:4796)
                                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                           at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:776)
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:543)
                                                                                           at dalvik.system.NativeStart.main(Native Method)
                                                                                        Caused by: java.lang.NullPointerException: CameraUpdateFactory is not initialized
                                                                                           at com.google.android.gms.common.internal.zzx.zzb(Unknown Source)
                                                                                           at com.google.android.gms.maps.CameraUpdateFactory.zzqf(Unknown Source)
                                                                                           at com.google.android.gms.maps.CameraUpdateFactory.newLatLngZoom(Unknown Source)
                                                                                           at com.chinatown.wangjian.chinatown_nearbyme.MainActivity.onCreate(MainActivity.java:71)
                                                                                           at android.app.Activity.performCreate(Activity.java:5031)
                                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)

Full code about CameraUpdateFactory as below , is there something wrong with this ?

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

        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Loading Nearest Restaurant...");
        pDialog.show();

        gps = new GPSTracker(this);

        textStatus = (TextView)findViewById(R.id.textStatus);
        listView = (ListView)findViewById(R.id.resturant_list);

        mMap = ((SupportMapFragment)getSupportFragmentManager()
                .findFragmentById(R.id.restaurant_map)).getMap();

        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(gps.getLatitude(), gps.getLongitude()), 15));


        gp = new GooglePlaceSearch(ApiKey);
        gp.setOnPlaceResponseListener(new OnPlaceResponseListener() {
            public void onResponse(String status, ArrayList<ContentValues> arr_data, Document doc) {
                Toast.makeText(getApplicationContext(), status, Toast.LENGTH_SHORT).show();
                textStatus.setText("Search Restaurant Nearby : " + status);
                pDialog.dismiss();

i found on this answer several times but always failure if there is something wrong with my code ? Please help Thx.

More Details for map

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(gps.getLatitude(), gps.getLongitude()), 15));


        gp = new GooglePlaceSearch(ApiKey);
        gp.setOnPlaceResponseListener(new OnPlaceResponseListener() {
            public void onResponse(String status, ArrayList<ContentValues> arr_data, Document doc) {
                Toast.makeText(getApplicationContext(), status, Toast.LENGTH_SHORT).show();
                textStatus.setText("Search Restaurant Nearby : " + status);
                pDialog.dismiss();

                if(status.equals(GooglePlaceSearch.STATUS_OK)) {
                    ArrayList<String> array = new ArrayList<String>();
                    final ArrayList<String> array_photo = new ArrayList<String>();

                    for(int i = 0 ; i < arr_data.size() ; i++) {
                        String title = arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_NAME);
                        double lat = arr_data.get(i).getAsDouble(GooglePlaceSearch.PLACE_LATITUDE);
                        double lng = arr_data.get(i).getAsDouble(GooglePlaceSearch.PLACE_LONGITUDE);
                        LatLng pos = new LatLng(lat, lng);

                        mMap.addMarker(new MarkerOptions().position(pos).title(title));

                        array.add("Name : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_NAME) + "\n"
                                + "Address : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_ADDRESS) + "\n"
                                + "Latitude : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_LATITUDE) + "\n"
                                + "Longitude : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_LONGITUDE) + "\n"
                                + "Phone Number : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_PHONENUMBER));
                        array_photo.add(arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_PHOTO));

                    }
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this
                            , R.layout.restaurant_listview_text, array);
                    listView.setAdapter(adapter);

                    listView.setOnItemClickListener(new OnItemClickListener() {
                        public void onItemClick(AdapterView<?> arg0, View arg1,
                                                int arg2, long arg3) {
                            Dialog dialog = new Dialog(MainActivity.this);
                            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                            dialog.setContentView(R.layout.restaurant_dialog_photo);
                            dialog.setCancelable(true);

                            final ImageView imgPhoto = (ImageView)dialog.findViewById(R.id.imgPhoto);

                            dialog.show();

                            gp.getPhotoBitmapByWidth(array_photo.get(arg2), 600, ""
                                    , new OnBitmapResponseListener() {
                                        public void onResponse(Bitmap bm, String tag) {
                                            imgPhoto.setImageBitmap(bm);
                                        }
                                    });
                        }
                    });
                }
            }
        });


        gp.getNearby(gps.getLatitude(), gps.getLongitude(), radius, type, language);
        //gp.getRadarSearch(latitude, longitude, radius, type, language, false);
    }
}
Tibiotarsus answered 1/6, 2016 at 16:29 Comment(0)
W
9

You need to wait for the map to load. Use this

((SupportMapFragment)getSupportFragmentManager()
        .findFragmentById(R.id.restaurant_map)).getMapAsync(new OnMapReadyCallback() {

        @Override
        public void onMapReady(GoogleMap googleMap) {
            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new 
                                    LatLng(gps.getLatitude(), gps.getLongitude()), 15));

                  mGoogleMap = googleMap;
                  // Rest of the stuff you need to do with the map
                }
            });
        }
Welterweight answered 1/6, 2016 at 16:46 Comment(3)
Thank you for quick reply. I don't understand what is 'mGoogleMap' use for. This is my first time use google map api so I'm a really newbie at this stuff. I will upload more code on the question. Could u please tell me where should i place 'OnMapReady'? Thanks again.Tibiotarsus
mGoogleMap is a global variable which you can use later. onMapReady is a callback.Welterweight
I insert below code to my onCreate. but i got many error. This problem has been bothering me a lot of days. Please Help me Thank you. mMap = ((SupportMapFragment)getSupportFragmentManager() .findFragmentById(R.id.restaurant_map)).getMapAsync(new OnMapReadyCallback() { @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(gps.getLatitude(), gps.getLongitude()), 15)); } });Tibiotarsus
S
1
    mMap = ((SupportMapFragment)getSupportFragmentManager()
            .findFragmentById(R.id.restaurant_map)).getMap();

    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(gps.getLatitude(), gps.getLongitude()), 15));

What they say is to replace this code with follow, you can try it and see if it will work. good luck

  ((SupportMapFragment)getSupportFragmentManager()
    .findFragmentById(R.id.restaurant_map)).getMapAsync(new OnMapReadyCallback() {

    @Override
    public void onMapReady(GoogleMap googleMap) {
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new 
                                LatLng(gps.getLatitude(), gps.getLongitude()), 15));

              mMap= googleMap;
              // Rest of the stuff you need to do with the map
            }
        });
    }
Salacious answered 6/6, 2016 at 8:50 Comment(0)
A
1

You need to wait for GoogleMap instance in the onMapReady callback Method.

@Override
public void onMapReady(GoogleMap googleMap) {
    mGooglemap = googleMap;
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(defaultLatLng, 10));
}
Antinomian answered 7/4, 2017 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.