How to place Mapactivity in a Fragment

Asked

Viewed 437 times

0

It is so, I have a Mapactivity, but I would like the map to be in a Fragment. How can I do this?

I created a Fragment, implemented the methods of Mapactivity created by Android Studio, but I come across an error. The getSupportFragment() method turns red and Android Studio only says "can’t 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 executing, 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) 
  • The mistake is really one NullPointerException on the line 45 class MapaFragment. What’s in this line of code ? Regardless, enjoy and read "What is Nullpointerexception and what are its main causes?"

  • This line 45 is as follows: mapFragment.getMapAsync(this); And I don’t understand how null is.

  • I suspect it’s yours findFragmentById who is returning null. Start by confirming whether the id of map_fragment is correct. And also confirm the import you are using if it is android.app.Fragment; or android.support.v4.app.Fragment;

2 answers

1


Thanks to everyone I’ve already made it! It was very simple, just catch this stretch:

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

take it out of the method onCreate(Bundle savedInstanceState) And put it on

onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)

That’s all it was!!! Thanks a lot!

-1

  • Whatever, put the link as a comment to the question @E FRANÇA

  • It is not available for me to make comments where it is not an answer or my question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.