【问题标题】:App is crashing when calling moveCamera Google maps调用 moveCamera Google 地图时应用程序崩溃
【发布时间】:2018-11-20 01:53:10
【问题描述】:

我正在尝试调用 moveCamera,但我的应用程序崩溃了。我收到了来自 Intent extra 的 latlng。我知道额外的东西即将到来,因为我打印它更新了一个编辑文本。我尝试过自己输入不同的坐标,但没有任何效果。这是我的错误

 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mmitm, PID: 28348
java.lang.RuntimeException: Unable to resume activity {com.example.mmitm/com.example.mmitm.MapActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.moveCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3645)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3685)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2898)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.moveCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference
    at com.example.mmitm.MapActivity.receiveData(MapActivity.java:136)
    at com.example.mmitm.MapActivity.onResume(MapActivity.java:111)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1354)
    at android.app.Activity.performResume(Activity.java:7079)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3620)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3685) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2898) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

这是我的地图活动,我称之为 movecamera

//onResume Method check which fragment intent is sent from
@Override
protected void onResume() {
    super.onResume();

    //make sure extras are not null
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        this.receiveData();
        Toast.makeText(this, "Received Data", Toast.LENGTH_SHORT).show();

    }
}

private void receiveData() {
    //RECEIVE DATA VIA INTENT
    Intent i = getIntent();

    if(i != null)
    {
        LatLng locationOne = i.getParcelableExtra("LOC_ONE");
        LatLng locationTwo = i.getParcelableExtra("LOC_TWO");

        Toast.makeText(this, "LocOne = " + locationOne + ", LocTwo = " + locationTwo, Toast.LENGTH_LONG).show();
        //SET DATA TO TEXTVIEWS
        locOne.setText(locationOne.toString());
        locTwo.setText(locationTwo.toString());
        // move camera to location one
        Log.d(TAG, "receiveData: calling moveCamera");



        //Map Crashes when trying to move camera
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(locationOne,15));
    }
}

还有我的 MoveCamera 功能

private void moveCamera(LatLng latLng, float zoom){
    Log.d(TAG, "moveCamera: moving the camera to: lat: " + latLng.latitude + ", lng: " + latLng.longitude );
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));
}

这是我的第一个 android 应用程序,因此感谢您的帮助!

【问题讨论】:

    标签: android google-maps-android-api-2


    【解决方案1】:

    它显然崩溃了,因为mMapNULL

    if(this.getIntent() != null && mMap != null) {
        ...
    }
    

    刚刚仔细看了看;你需要处理意图onMapReady()

    @Override
    public void onMapReady(GoogleMap map) {
    
        this.mMap = map;
    
        if(this.getIntent() != null && this.getIntent().getAction() == INTENT_ACTION_LOCATION_SELECT) {
            /* your code goes here */
        } else {
            /* regular initialization */
        }
    }
    

    onResume() 可能需要从SupportMapFragment 获取地图。

    【讨论】:

      【解决方案2】:

      您在函数本身内部调用 moveCamera,除了当有内置函数时为什么需要自己的函数。 mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 15.0f));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多