Android Google Maps CameraUpdateFactory未初始化

Wan*_*ian 3 android google-maps google-maps-api-2 android-fragments google-play-services

我希望ro在地图上显示一个点作为我的位置,但是当我将相机设置在地图上时,为什么会发生此错误?

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)
Run Code Online (Sandbox Code Playgroud)

有关CameraUpdateFactory的完整代码如下,这有什么问题吗?

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)
Run Code Online (Sandbox Code Playgroud)

我多次在此答案上发现,但是如果我的代码有问题,总是会失败?请帮助Thx。

地图的更多详细信息

@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();
Run Code Online (Sandbox Code Playgroud)

Uda*_*day 5

您需要等待地图加载。用这个

((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
                }
            });
        }
Run Code Online (Sandbox Code Playgroud)