Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google Maps CameraUpdateFactory not initialized

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);
    }
}
like image 977
Wang Jian Avatar asked Mar 12 '23 03:03

Wang Jian


1 Answers

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
                }
            });
        }
like image 178
Udayaditya Barua Avatar answered Mar 26 '23 19:03

Udayaditya Barua