Asdemuertes Asdemuertes -4 years ago 284
Android Question

google maps in fragment android

I'm having problems loading a google Maps in my fragment. Everything starts in a recycler view adapter that launchs the activity "ViatgeDetallViewActivity" :

Context context = mActivity.getApplicationContext();
Intent intent = new Intent(context, ViatgeDetallViewActivity.class);
holder.mViatge.getId());
intent.putExtra("desc",p.getDesc());
intent.putExtra("location",p.getLocation());
mActivity.startActivityForResult(intent,ACTIVITY_View);


This is my DetallViewActivity :

public class ViatgeDetallViewActivity extends AppCompatActivity {

ViatgeDetallViewFragment fragment;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
Bundle extras = getIntent().getExtras();
if(extras != null)
{

String s = getIntent().getStringExtra("desc");
Location l = getIntent().getParcelableExtra("location");

Log.d("myTag", "estoy en el oncreate de detallviewActivity pasando pro extras" + s );


}

fragment = new ViatgeDetallViewFragment();


getSupportFragmentManager().beginTransaction()
.add(R.id.activity_fragment_view, fragment)
.commit();
getFragmentManager().beginTransaction().add(R.id.activity_fragment_view, fragment).commit();
}
}


With my ViatgeDetallViewFragment being :

public class ViatgeDetallViewFragment extends Fragment implements OnMapReadyCallback {

private GoogleMap mMap;
private Marker mMarcadorActual;

public ViatgeDetallViewFragment() {
}
....
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_view, container, false);


SupportMapFragment mapFragment =
(SupportMapFragment)
getChildFragmentManager().findFragmentById(R.id.fragment_view_map);
mapFragment.getMapAsync(this);


return rootView;
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;


//ask for permissions..
mMap.setMyLocationEnabled(true);


LatLng sydney = new LatLng(-34, 151);
mMarcadorActual = mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));

}


With the layouts being , Activity_view:

<Linear ...

<android.support.v7.widget.Toolbar
android:id="@+id/tlbMenuView"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
/>

<FrameLayout
android:id="@+id/activity_fragment_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>


fragment_view:

<LinearLayout android:id="@+id/layout_fragment_view" xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
..
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragment_view_map"
class="com.google.android.gms.maps.MapFragment"></fragment>

</LinearLayout>


The problem comes when i try launch tje "ViatgeDetallActivity" , i get this in the log:


FATAL EXCEPTION: main
Process: com.example.usuari.myapplication3, PID: 6390
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.usuari.myapplication3/com.example.usuari.myapplication3.ViatgeDetallViewActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)'
on a null object reference
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'void
com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)'
on a null object reference
at
com.example.usuari.myapplication3.ViatgeDetallViewFragment.onCreateView(ViatgeDetallViewFragment.java:82)
at
android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at
android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at
android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
at
android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at
android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:619)
at
android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1265)
at android.app.Activity.performStart(Activity.java:6915)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3217)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 
at android.app.ActivityThread.access$1100(ActivityThread.java:222) 
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7229) 
at java.lang.reflect.Method.invoke(Native Method) 
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
03-24 02:55:53.871 6390-7362/com.example.usuari.myapplication3
W/DynamiteModule: Local module descriptor class for
com.google.android.gms.googlecertificates not found. 03-24
02:55:53.881 6390-7362/com.example.usuari.myapplication3
I/DynamiteModule: Considering local module
com.google.android.gms.googlecertificates:0 and remote module
com.google.android.gms.googlecertificates:2 03-24 02:55:53.881
6390-7362/com.example.usuari.myapplication3 I/DynamiteModule: Selected
remote version of com.google.android.gms.googlecertificates, version


= 2 03-24 02:55:53.881 6390-7362/com.example.usuari.myapplication3 W/ResourcesManager: getTopLevelResources:
/data/user/0/com.google.android.gms/app_chimera/m/00000005/DynamiteModulesA_GmsCore_prodmnc_alldpi_release.apk
/ 1.0 running in com.example.usuari.myapplication3 rsrc of package
com.example.usuari.myapplication3 03-24 02:55:53.891
6390-7362/com.example.usuari.myapplication3 D/ResourcesManager: For
user 0 new overlays fetched Null 03-24 02:55:53.891
6390-7362/com.example.usuari.myapplication3 W/ResourcesManager:
getTopLevelResources:
/data/user/0/com.google.android.gms/app_chimera/m/00000005/DynamiteModulesA_GmsCore_prodmnc_alldpi_release.apk
/ 1.0 running in com.example.usuari.myapplication3 rsrc of package
com.example.usuari.myapplication3


Answer Source

Your mapFragment is null. Try this:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
 super.onViewCreated(view, savedInstanceState);
MapFragment mapFragment = (MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.fragment_view_map);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download