Mahmud Hasan
Mahmud Hasan

Reputation: 33

multiple location show in google map using android

When i run project it crashes with following error is Unable to start activity java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference. Can anyone help me for solve this problem or another idea for creating multiple location in android google Map activity.

                                                                                    --------- beginning of crash
08-10 12:57:13.476 3250-3250/daffodilvarsity.edu.bd.studentportal E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: daffodilvarsity.edu.bd.studentportal, PID: 3250
                                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{daffodilvarsity.edu.bd.studentportal/daffodilvarsity.edu.bd.studentportal.LocationShareMapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference

Logcat error is

                                                                                --------- beginning of crash
08-10 12:57:13.476 3250-3250/daffodilvarsity.edu.bd.studentportal E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: daffodilvarsity.edu.bd.studentportal, PID: 3250
                                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{daffodilvarsity.edu.bd.studentportal/daffodilvarsity.edu.bd.studentportal.LocationShareMapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                    at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:148)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
                                                                                    at daffodilvarsity.edu.bd.studentportal.LocationShareMapsActivity.plotMarkers(LocationShareMapsActivity.java:107)
                                                                                    at daffodilvarsity.edu.bd.studentportal.LocationShareMapsActivity.onCreate(LocationShareMapsActivity.java:54)
                                                                                    at android.app.Activity.performCreate(Activity.java:6237)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                    at android.os.Looper.loop(Looper.java:148) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
08-10 12:57:15.409 3250-3494/daffodilvarsity.edu.bd.studentportal W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found.
08-10 12:57:15.410 3250-3494/daffodilvarsity.edu.bd.studentportal I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:1
08-10 12:57:15.410 3250-3494/daffodilvarsity.edu.bd.studentportal I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 1
08-10 12:57:15.433 3250-3494/daffodilvarsity.edu.bd.studentportal D/ChimeraFileApk: Primary ABI of requesting process is x86_64
08-10 12:57:15.434 3250-3494/daffodilvarsity.edu.bd.studentportal D/ChimeraFileApk: Classloading successful. Optimized code found.
08-10 12:57:15.434 3250-3494/daffodilvarsity.edu.bd.studentportal D/GoogleCertificates: com.google.android.gms.googlecertificates module is loaded
08-10 12:57:15.495 3250-3494/daffodilvarsity.edu.bd.studentportal D/GoogleCertificatesImpl: Fetched 318 Google certificates
08-10 13:15:06.095 7141-7141/daffodilvarsity.edu.bd.studentportal I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
08-10 13:15:06.096 7141-7141/daffodilvarsity.edu.bd.studentportal I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
08-10 13:15:06.108 7141-7155/daffodilvarsity.edu.bd.studentportal D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                                                [ 08-10 13:15:06.112  7141: 7141 D/         ]
                                                                                HostConnection::get() New Host Connection established 0x7fcde51d17c0, tid 7141


                                                                                [ 08-10 13:15:06.181  7141: 7155 D/         ]
                                                                                HostConnection::get() New Host Connection established 0x7fcde8d90300, tid 7155
08-10 13:15:06.187 7141-7155/daffodilvarsity.edu.bd.studentportal I/OpenGLRenderer: Initialized EGL, version 1.4
08-10 13:15:07.444 7141-7141/daffodilvarsity.edu.bd.studentportal I/Choreographer: Skipped 71 frames!  The application may be doing too much work on its main thread.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<permission
    android:name="com.example.mapexdemo.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".LoginActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".LocationShareMapsActivity"
        android:label="@string/title_activity_location_share_maps" />
    <meta-data
        android:name="com.google.android.gms.vision"
        android:value="6587000" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyCBe_7AckAvLMRIryGwRSvo-lhpv-VTbSY" />
</application>
</manifest>

This is Biuld.gradle file

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "24.0.1"
useLibrary 'org.apache.http.legacy'

dexOptions {
    incremental true
    javaMaxHeapSize "2048M"
}

defaultConfig {
    applicationId "daffodilvarsity.edu.bd.studentportal"
    minSdkVersion 14
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"

        // Enabling multidex support.
        multiDexEnabled true

}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.google.android.gms:play-services:9.2.1'
compile 'com.google.android.gms:play-services-maps:4.3.23'
}

Here is My main Java file LocationShareMapsActivity.java

package daffodilvarsity.edu.bd.studentportal;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import java.util.ArrayList;
import java.util.HashMap;

public class LocationShareMapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private ArrayList<MyMarker> mMyMarkersArray = new ArrayList<MyMarker>();
private HashMap<Marker, MyMarker> mMarkersHashMap;

public LocationShareMapsActivity() {
    //empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location_share_maps);

    // Initialize the HashMap for Markers and MyMarker object
    mMarkersHashMap = new HashMap<Marker, MyMarker>();

    mMyMarkersArray.add(new MyMarker("Brasil", "icon1", Double.parseDouble("-28.5971788"), Double.parseDouble("-52.7309824")));
    mMyMarkersArray.add(new MyMarker("United States", "icon2", Double.parseDouble("33.7266622"), Double.parseDouble("-87.1469829")));
    mMyMarkersArray.add(new MyMarker("Canada", "icon3", Double.parseDouble("51.8917773"), Double.parseDouble("-86.0922954")));
    mMyMarkersArray.add(new MyMarker("England", "icon4", Double.parseDouble("52.4435047"), Double.parseDouble("-3.4199249")));
    mMyMarkersArray.add(new MyMarker("España", "icon5", Double.parseDouble("41.8728262"), Double.parseDouble("-0.2375882")));
    mMyMarkersArray.add(new MyMarker("Portugal", "icon6", Double.parseDouble("40.8316649"), Double.parseDouble("-4.936009")));
    mMyMarkersArray.add(new MyMarker("Deutschland", "icon7", Double.parseDouble("51.1642292"), Double.parseDouble("10.4541194")));
    mMyMarkersArray.add(new MyMarker("Atlantic Ocean", "icondefault", Double.parseDouble("-13.1294607"), Double.parseDouble("-19.9602353")));

    setUpMap();

    plotMarkers(mMyMarkersArray);
}

private void setUpMap()  {

    if (mMap == null) {

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        // Check if we were successful in obtaining the map.

        if (mMap != null)  {
            mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()   {
                @Override
                public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker)
                {
                    marker.showInfoWindow();
                    return true;
                }
            });
        }
    }
        else
            Toast.makeText(getApplicationContext(), "Unable to create Maps", Toast.LENGTH_SHORT).show();

}

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

private void plotMarkers(ArrayList<MyMarker> markers)
{
    if(markers.size() > 0)
    {
        for (MyMarker myMarker : markers)   {
            MarkerOptions markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
            markerOption.icon(BitmapDescriptorFactory.fromResource(R.drawable.currentlocation_icon));

            Marker currentMarker = mMap.addMarker(markerOption);
            mMarkersHashMap.put(currentMarker, myMarker);
            mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
        }
    }
}

private int manageMarkerIcon(String markerIcon) {
    if (markerIcon.equals("icon1"))
        return R.drawable.icon1;
    else if(markerIcon.equals("icon2"))
        return R.drawable.icon2;
    else if(markerIcon.equals("icon3"))
        return R.drawable.icon3;
    else if(markerIcon.equals("icon4"))
        return R.drawable.icon4;
    else if(markerIcon.equals("icon5"))
        return R.drawable.icon5;
    else if(markerIcon.equals("icon6"))
        return R.drawable.icon6;
    else if(markerIcon.equals("icon7"))
        return R.drawable.icon7;
    else
        return R.drawable.icondefault;
}

public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter  {
    public MarkerInfoWindowAdapter() {
    }

    @Override
    public View getInfoWindow(Marker marker)    {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker)     {
        View v  = getLayoutInflater().inflate(R.layout.infowindow_layout, null);

        MyMarker myMarker = mMarkersHashMap.get(marker);

        ImageView markerIcon = (ImageView) v.findViewById(R.id.marker_icon);

        TextView markerLabel = (TextView)v.findViewById(R.id.marker_label);

        TextView anotherLabel = (TextView)v.findViewById(R.id.another_label);


           markerIcon.setImageResource(manageMarkerIcon(myMarker.getmIcon()));

        markerLabel.setText(myMarker.getmLabel());
        anotherLabel.setText("A custom text");

        return v;
    }
 }
}

this is MyMaker.java file for getter and setter

package daffodilvarsity.edu.bd.studentportal;
public class MyMarker{
private String mLabel;
private String mIcon;
private Double mLatitude;
private Double mLongitude;

public MyMarker(String label, String icon, Double latitude, Double longitude)  {
    this.mLabel = label;
    this.mLatitude = latitude;
    this.mLongitude = longitude;
    this.mIcon = icon;
}

public String getmLabel() {
    return mLabel;
}

public void setmLabel(String mLabel) {
    this.mLabel = mLabel;
}

public String getmIcon() {
    return mIcon;
}

public void setmIcon(String icon) {
    this.mIcon = icon;
}

public Double getmLatitude() {
    return mLatitude;
}

public void setmLatitude(Double mLatitude) {
    this.mLatitude = mLatitude;
}

public Double getmLongitude() {
    return mLongitude;
}

public void setmLongitude(Double mLongitude) {
    this.mLongitude = mLongitude;
}
}

This is activity_location_share_maps.xml file for showing map

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="daffodilvarsity.edu.bd.studentportal.LocationShareMapsActivity" />

infowindow_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/marker_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
    android:id="@+id/station_info_layout"
    android:layout_width="205dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

        <ImageView
            android:id="@+id/marker_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/marker_label"
            android:layout_marginLeft="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold" />

</LinearLayout>

<TextView
    android:id="@+id/another_label"
    android:layout_marginLeft="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textStyle="bold" />
</LinearLayout>

Upvotes: 0

Views: 346

Answers (1)

Arun Mehra
Arun Mehra

Reputation: 559

Plot your markers after you map is ready. What you are doing is here is checking if your mMap is null or not but you are plotting your markers in onCreate. The map is an async call so it is moved to a background thread while you are still trying to plot your markers on the UI thread. Just plot the markers after your map is ready. I hope that will solve your problem.

Upvotes: 1

Related Questions