(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
mainactivity.java
package com.example.neetu.mapexample; import android.app.actionbar; import android.location.address; import android.location.geocoder; import android.os.asynctask; import android.support.v4.app.fragmentactivity; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.toast; import com.google.android.gms.maps.cameraupdatefactory; 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.latlng; import com.google.android.gms.maps.model.markeroptions; import java.io.ioexception; import java.util.list; public class mainactivity extends fragmentactivity implements onmapreadycallback { googlemap googlemap; markeroptions markeroptions; latlng latlng; edittext etlocation; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); supportmapfragment mapfragment = (supportmapfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.map); mapfragment.getmapasync(this); button btn_find = (button) findviewbyid(r.id.btn_find); etlocation = (edittext) findviewbyid(r.id.et_location); view.onclicklistener findclicklistener = new view.onclicklistener() { @override public void onclick(view v) { string location = etlocation.gettext().tostring(); if(location!=null && !location.equals("")){ new geocodertask().execute(location); } } }; btn_find.setonclicklistener(findclicklistener); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. //getmenuinflater().inflate(r.menu.activity_main, menu); return true; } @override public void onmapready(googlemap googlemap) { } private class geocodertask extends asynctask<string, void, list<address>> { @override protected list<address> doinbackground(string... locationname) { // creating instance of geocoder class geocoder geocoder = new geocoder(getbasecontext()); list<address> addresses = null; try { // getting maximum of 3 address matches input text addresses = geocoder.getfromlocationname(locationname[0], 3); } catch (ioexception e) { e.printstacktrace(); } //toast.maketext(mainactivity.this, (charsequence) addresses, toast.length_short).show(); return addresses; } @override protected void onpostexecute(list<address> addresses) { if(addresses==null || addresses.size()==0) { toast.maketext(getbasecontext(), "no location found", toast.length_short).show(); } // clears existing markers on map //googlemap.clear(); // adding markers on google map each matching address for(int i=0;i<addresses.size();i++){ address address = (address) addresses.get(i); // creating instance of geopoint, display in google map latlng = new latlng(address.getlatitude(), address.getlongitude()); string addresstext = string.format("%s, %s", address.getmaxaddresslineindex() > 0 ? address.getaddressline(0) : "", address.getcountryname()); toast.maketext(mainactivity.this, addresstext, toast.length_short).show(); googlemap.addmarker(new markeroptions().position(latlng).title(addresstext)); if(i==0) googlemap.animatecamera(cameraupdatefactory.newlatlng(latlng)); } } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout 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:orientation="vertical" tools:context=".mainactivity" > <relativelayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <button android:id="@+id/btn_find" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/str_btn_find" android:layout_alignparentright="true" /> <edittext android:id="@+id/et_location" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputtype="text" android:hint="@string/hnt_et_location" android:layout_toleftof="@id/btn_find" /> </relativelayout> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.supportmapfragment" /> </linearlayout>
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.neetu.mapexample"> <!-- access_coarse/fine_location permissions not required use google maps android api v2, must specify either coarse or fine location permissions 'mylocation' functionality. --> <uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.write_external_storage"/> <uses-permission android:name="com.google.android.providers.gsf.permission.read_gservices"/> <uses-permission android:name="android.permission.access_coarse_location"/> <uses-permission android:name="android.permission.access_fine_location"/> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundicon="@mipmap/ic_launcher_round" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <!-- api key google maps-based apis defined string resource. (see file "res/values/google_maps_api.xml"). note api key linked encryption key used sign apk. need different api key each encryption key, including release key used sign apk publishing. can define keys debug , release targets in src/debug/ , src/release/. --> <meta-data android:name="com.google.android.geo.api_key" android:value="@string/google_maps_key" /> <activity android:name=".mapsactivity" android:label="@string/title_activity_maps"></activity> </application> </manifest>
i try mark , display place input place name in edit text here error occur. please me.
locat
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 @ com.example.neetu.mapexample.mainactivity$geocodertask.onpostexecute(mainactivity.java:119) @ com.example.neetu.mapexample.mainactivity$geocodertask.onpostexecute(mainactivity.java:73)
it because haven't assigned value googlemap
field yet. update value when ready.
@override public void onmapready(googlemap googlemap){ this.googlemap = googlemap; }
also add googlemap != null
contition in click listener.
if(googlemap!=null && location!=null && !location.equals("")){ new geocodertask().execute(location); }
Comments
Post a Comment