Android application crashes when open map activity for multiple location -
i want show multiple location in map activity, every thing ok. cannot sun application. crashes when run on emulator. logcat error show :
java.lang.runtimeexception: unable start activity componentinfo{daffodilvarsity.edu.bd.studentportal/daffodilvarsity.edu.bd.studentportal.locationsharemapsactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'com.google.android.gms.maps.model.marker com.google.android.gms.maps.googlemap.addmarker(com.google.android.gms.map s.model.markeroptions)' on null object reference
this gradle.build 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" 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 'org.apache.httpcomponents:httpcore:4.4.1' compile 'com.loopj.android:android-async-http:1.4.9' }
this andridmanifest.xml file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="daffodilvarsity.edu.bd.studentportal"> <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=".locationsharemapsactivity" android:label="@string/title_activity_location_share_maps" /> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <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 activity_location_share_maps.xml layout file
<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 file
<?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>
locationsharemapsactivity.java file
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 hashmap markers , 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(){ // null check confirm have not instantiated map. if (mmap == null) { // try obtain map supportmapfragment. supportmapfragment mapfrag = (supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map); mapfrag.getmapasync(this); 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 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) { (mymarker mymarker : markers) { // create user marker custom icon , other options 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; } } }
mymaker.java file
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; } }
logcat error message:
08-08 15:44:37.432 17002-17002/daffodilvarsity.edu.bd.studentportal e/androidruntime: fatal exception: main process: daffodilvarsity.edu.bd.studentportal, pid: 17002 java.lang.runtimeexception: unable start activity componentinfo{daffodilvarsity.edu.bd.studentportal/daffodilvarsity.edu.bd.studentportal.locationsharemapsactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'com.google.android.gms.maps.model.marker com.google.android.gms.maps.googlemap.addmarker(com.google.android.gms.map s.model.markeroptions)' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2416) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2476) @ android.app.activitythread.-wrap11(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1344) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) caused by: java.lang.nullpointerexception: attempt 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 null object reference @ daffodilvarsity.edu.bd.studentportal.locationsharemapsactivity.plotmarkers(locationsharemapsactivity.java:107) @ daffodilvarsity.edu.bd.studentportal.locationsharemapsactivity.oncreate(locationsharemapsactivity.java:54) @ android.app.activity.performcreate(activity.java:6237) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1107 ) @ android.app.activitythread.performlaunchactivity(activitythread.java:2369) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2476) @ android.app.activitythread.-wrap11(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1344) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) 08-08 15:44:39.431 17002-17885/daffodilvarsity.edu.bd.studentportal w/dynamitemodule: local module descriptor class com.google.android.gms.googlecertificates not found. 08-08 15:44:39.432 17002-17885/daffodilvarsity.edu.bd.studentportal i/dynamitemodule: considering local module com.google.android.gms.googlecertificates:0 , remote module com.google.android.gms.googlecertificates:1 08-08 15:44:39.432 17002-17885/daffodilvarsity.edu.bd.studentportal i/dynamitemodule: selected remote version of com.google.android.gms.googlecertificates, version >= 1 08-08 15:44:39.433 17002-17885/daffodilvarsity.edu.bd.studentportal d/chimerafileapk: primary abi of requesting process x86_64 08-08 15:44:39.433 17002-17885/daffodilvarsity.edu.bd.studentportal d/chimerafileapk: classloading successful. optimized code found. 08-08 15:44:39.434 17002-17885/daffodilvarsity.edu.bd.studentportal d/googlecertificates: com.google.android.gms.googlecertificates module loaded 08-08 15:44:39.460 17002-17885/daffodilvarsity.edu.bd.studentportal d/googlecertificatesimpl: fetched 318 google certificates
Comments
Post a Comment