-1

I'm trying to make a marker on Maps with data lat and long from web service, but it returns error null pointer. I do make log data and the lat and long data are retrieved on log successfully. Please help. Thank you in advance.

This is my KDetail.java

 private PData data;

private static final String TAG = "KDActivity";
private Call<APIBaseResponse> call;
private RestClient.GitApiInterface service;

private Toolbar toolbar;

private SessionManager sessions;

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

    sessions = new SessionManager(this);
    Intent i = getIntent();
    data = (PData) i.getSerializableExtra("PDItem");

    txtIdP = (TextView) findViewById(R.id.txtIdP); 
    txtCdt = (EditText) findViewById(R.id.txtCreatedDtm);  

    txtIdP.setText(data.getId_P()); 
    txtCdt.setText(data.getCreatedDtm());      

    final MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            Log.d(TAG, "onMapReady: "); 
            double lata = Double.parseDouble(data.getLatP());
            double lana = Double.parseDouble(data.getLangP());

            googleMap.addMarker(new MarkerOptions()
                    .position(new LatLng(lata, lana))
                    .title("Start") );
        }
    });

}

This is the error :

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
                                                                            at com.kd.kddupe.Activity.KDetail.onCreate(KDetail.java:115)

Line 115 is :

mapFragment.getMapAsync(new OnMapReadyCallback() {

This is my acitivity_k_d.xml :

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@color/feed_bg">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/view">
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay" >
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>
        </android.support.design.widget.AppBarLayout>
    </LinearLayout>


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@drawable/bg_parent_rounded_corner"
            android:elevation="6pt"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="@dimen/feed_item_margin"
            android:layout_marginRight="@dimen/feed_item_margin"
            android:layout_marginTop="@dimen/feed_item_margin"
            android:paddingBottom="@dimen/feed_item_padding_top_bottom"
            android:paddingTop="@dimen/feed_item_padding_top_bottom" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="@string/id_p"
                android:id="@+id/txtIdP"
                android:textColor="@color/colorPrimaryDark"
                android:textStyle="bold"
                android:textSize="18sp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="20dp"
                android:textAlignment="center"/>

            <View
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:background="@color/colorPrimaryDark"
                android:id="@+id/line1" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="250dp">
                <fragment android:id="@+id/map"
                   android:name="com.google.android.gms.maps.SupportMapFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
            </LinearLayout>


            <View
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:background="@color/colorPrimaryDark"
                android:id="@+id/line2" />

            <LinearLayout
                android:orientation="horizontal"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                android:layout_marginTop="20dp" >
                <LinearLayout
                    android:layout_weight="2"
                    android:layout_height="fill_parent"
                    android:layout_width="0dp"
                    android:orientation="vertical"
                    android:paddingLeft="10dp">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="@string/created"
                        android:textSize="15sp"
                        android:gravity="center_vertical" />
                </LinearLayout>
                <LinearLayout
                    android:layout_weight="4"
                    android:layout_height="fill_parent"
                    android:layout_width="0dp"
                    android:orientation="vertical"
                    android:paddingRight="10dp"
                    android:paddingLeft="5dp">
                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/txtCreatedDtm"
                        android:background="@drawable/bg_parent_rounded_corner"
                        android:hint="@string/yyyy_mm_dd"
                        android:inputType="textMultiLine"
                        android:minHeight="35dp"
                        android:textSize="15sp"
                        android:singleLine="false"
                        android:focusable="false"/>
                </LinearLayout>
            </LinearLayout>

        </LinearLayout>
    </ScrollView>
</RelativeLayout>

2 Answers2

0

Check is may be getSupportFragmentManager() or check the id is it map for sure?? Try it instead of getFragmentManager

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
0

OK here the problem at your XML file you using the support version of the map fragment and the code you search for the normal one. so to fix it you have two solutions

choose one from the two

1- change the name of the fragment at xml to

    android:name="com.google.android.gms.maps.MapFragment"

2- change the MapFragment to SupportMapFragment at your java code

Ahmed Abdelmeged
  • 2,299
  • 2
  • 18
  • 29