从活动中移动Mapfragment中的摄像头不起作用

时间:2017-03-14 08:06:16

标签: android dictionary mapfragment

我有一个mapFragment,我想从我的活动中控制地图......例如。移动相机等,我还需要将Data von Activity推送到片段(例如LatLng)来更改Map。

这是我的代码和失败我试图运行应用程序:

主要活动

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mapFragment mapFragment = new mapFragment();

    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.add(R.id.content_main, mapFragment, "myMapFragement");
    transaction.commit();

    mapFragment.moveCamera(new LatLng(-3, 12));


}

MapFragment

public class mapFragment extends Fragment implements OnMapReadyCallback{

GoogleMap mGoogleMap;
MapView mMapView;
View mView;

//Konstruktor
public mapFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    mView =  inflater.inflate(R.layout.fragment_map, container, false);
    return mView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    mMapView = (MapView) mView.findViewById(R.id.mapView);
    if(mMapView != null){
        mMapView.onCreate(null);
        mMapView.onResume();
        mMapView.getMapAsync(this);
    }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    MapsInitializer.initialize(getContext());

    mGoogleMap = googleMap;
    mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    // Add a marker in Sydney, Australia, and move the camera.
    //LatLng sydney = new LatLng(-1, 151);
    mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(-1, 55)).title("Marker in Sydney"));
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(-1, 55)));

}

public void moveCamera(LatLng latLng){
    if (mGoogleMap == null){
        Log.i("map", "null");
    }
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mGoogleMap.addMarker(new MarkerOptions().position(latLng).title("elsewhere"));

}

运行应用时错误

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: ibas.locatix_teamviewer, PID: 3003
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{ibas.locatix_teamviewer/ibas.locatix_teamviewer.MainActivity}: java.lang.NullPointerException: CameraUpdateFactory is not initialized
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                   Caused by: java.lang.NullPointerException: CameraUpdateFactory is not initialized
                      at com.google.android.gms.common.internal.zzac.zzb(Unknown Source)
                      at com.google.android.gms.maps.CameraUpdateFactory.zzJm(Unknown Source)
                      at com.google.android.gms.maps.CameraUpdateFactory.newLatLng(Unknown Source)
                      at ibas.locatix_teamviewer.mapFragment.moveCamera(mapFragment.java:78)
                      at ibas.locatix_teamviewer.MainActivity.onCreate(MainActivity.java:52)
                      at android.app.Activity.performCreate(Activity.java:6679)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Application terminated.

任何想法如何处理?

1 个答案:

答案 0 :(得分:0)

首先,在onCreateView中获取MapView:

mMapView = (MapView) mView.findViewById(R.id.mapView);

然后,在onCreateView中初始化地图:

 try {
        MapsInitializer.initialize(getActivity());
    }
    catch (GooglePlayServicesNotAvailableException e) {
        Log.e(LOG_TAG, "Have GoogleMap but then error", e);
        return;
    }

最后,错误发生在MainActivity(参见logcat):

  

MainActivity.onCreate(MainActivity.java:52)

从活动中删除以下行:

mapFragment.moveCamera(new LatLng(-3, 12));

这导致错误,因为您在Map准备好之前请求moveCamera。

感谢this