1

I want to put a google map in a static cardview, with other elements like button, textview, etc.

so I create this xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/prenotation"
>

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal|bottom">

        <com.google.android.gms.maps.MapView
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:enabled="true" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="LIMB"
        android:textColor="#000"
        android:textSize="30dp"
        android:id="@+id/and"
        android:layout_marginTop="40dp"
        android:layout_below="@+id/map"
        android:layout_alignLeft="@+id/view1"
        android:layout_alignStart="@+id/view1"
        android:layout_marginLeft="30dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="LIPU"
        android:textColor="#000"
        android:textSize="30dp"
        android:id="@+id/rit"
        android:layout_alignTop="@+id/and"
        android:layout_alignRight="@+id/view1"
        android:layout_alignEnd="@+id/view1"
        android:layout_marginRight="30dp"
        android:layout_marginEnd="30dp" />
 </RelativeLayout>
</android.support.v7.widget.CardView>

and this in Java:

   public class PrenotationFragment extends Fragment
   {
    String objID;
    MapView mMapView;
    GoogleMap mgoogleMap;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.fragment_prenotation, container, false);
        Bundle args = getArguments();
        Button prenota = (Button)rootView.findViewById(R.id.prenotate);
        if(args != null)
        {
            objID = args.getString("Obj");

            Download task = new Download(objID,getActivity(),rootView);
            task.execute();

        }
        prenota.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                String url = "http://www.aviosharing.eu/?flight=("+objID+")";
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);
            }
        });

        mMapView = (MapView) rootView.findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);
        mMapView.onResume();// needed to get the map to display immediately
        try
        {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        mMapView.getMapAsync(new OnMapReadyCallback()
        {

            public void onMapReady(GoogleMap googleMap)
            {
                mgoogleMap = googleMap;
            }
        });
        return rootView;
    }
       public void onResume() {
        super.onResume();
        mMapView.onResume();
    }
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

but when I load this I obtain this error message:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapView.onCreate(android.os.Bundle)' on a null object reference at com.example.carsharing.PrenotationFragment.onCreateView(PrenotationFragment.java:79) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5253) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

why I get this error? I think also, I need to use :

   <fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment" />

instead of MapView in this case?

Thanks

ste9206
  • 1,822
  • 4
  • 31
  • 47

0 Answers0