Placing MapActivity in a Fragment

0

So, I have a MapActivity, but I'd like the map to be in a Fragment. How can I do this?

I created a Fragment, implemented the MapActivity methods created by Android Studio, but I run into an error. The getSupportFragment () method turns red and Android Studio says "can not solve the method (...)"

public class MapaFragment extends Fragment implements OnMapReadyCallback{


    private View view = null;
    private GoogleMap mMap;


    public MapaFragment() {
        // Required empty public constructor
    }


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

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager()
                .findFragmentById(R.id.map_fragment);
        mapFragment.getMapAsync(this);


    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        view = inflater.inflate(R.layout.activity_maps, container, false);

        return view;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

        mMap = googleMap;

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


    }

}

And when running, it gives the following error:

04-20 11:48:44.769 28258-28258/mz.co.macave.bmtrack E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
04-20 11:48:46.781 28258-28258/mz.co.macave.bmtrack E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: mz.co.macave.bmtrack, PID: 28258
                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{mz.co.macave.bmtrack/mz.co.macave.bmtrack.MainActivity}: java.lang.NullPointerException
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                          at android.os.Looper.loop(Looper.java:193)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5299)
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:515)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
                                                                          at dalvik.system.NativeStart.main(Native Method)
                                                                       Caused by: java.lang.NullPointerException
                                                                          at mz.co.macave.bmtrack.MapaFragment.onCreate(MapaFragment.java:45)
                                                                          at android.support.v4.app.Fragment.performCreate(Fragment.java:2226)
                                                                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
                                                                          at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1574)
                                                                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1641)
                                                                          at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794)
                                                                          at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2415)
                                                                          at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2200)
                                                                          at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2153)
                                                                          at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2063)
                                                                          at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                                                                          at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:554)
                                                                          at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
                                                                          at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174)
                                                                          at android.app.Activity.performStart(Activity.java:5274)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                          at android.os.Looper.loop(Looper.java:193) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5299) 
                                                                          at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                          at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) 
                                                                          at dalvik.system.NativeStart.main(Native Method) 
    
asked by anonymous 20.04.2018 / 11:58

2 answers

1

Thanks to everyone, I already have! It was very simple, just get this section:

SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager()
                .findFragmentById(R.id.map_fragment);
        mapFragment.getMapAsync(this);

Remove it from the method onCreate(Bundle savedInstanceState) And put it on

onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)

That's it !!! Thanks guys!

    
24.04.2018 / 08:42
-1

This is not quite a response but an indication to do exactly what you want: link .

    
24.04.2018 / 06:59