android - when button is clicked map doesn't work getting (NullPointerException) -


i have created button in class lead map in class when clicked button crashes , error(nullpointerexception), want display map when button clicked , code

    package com.example.gmapsapp;  import java.util.stack;  import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.util.log; import android.widget.toast;  import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.mapfragment; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.markeroptions; import com.google.android.gms.maps.model.polylineoptions;  public class airports extends fragmentactivity {     googlemap googlemap;     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.airports);         createmapview();         tspnearestneighbour tsp = new tspnearestneighbour();         int[] shortest = new int[21];         string[] placenames = {"cairo international airport","alexandria international airport","borg el arab airport",                 "marsa matrouh airport","sharm el-sheikh international airport","taba international airport","el kharga airport",                 "assiut airport","luxor international airport","aswan international airport","el arish international airport",                 "st. catherine international airport","sharq al-owainat airport","abu simbel airport","sohag international airport",                 "port said airport","el tor airport","dakhla oasis airport","marsa alam international airport","cairo west air base","almaza air force base"};         string[] placenamessnippet = {"cairo international airport1","alexandria international airport2","borg el arab airport3", "marsa matrouh airport4","sharm el-sheikh international airport5","taba international airport6","el kharga airport7", "assiut airport8","luxor international airport9","aswan international airport10","el arish international airport11", "st. catherine international airport12","sharq al-owainat airport13","abu simbel airport14","sohag international airport15", "port said airport16","el tor airport17","dakhla oasis airport18","marsa alam international airpor19t","cairo west air bas20e","almaza air force base21"};         double[] placelatitude =  {30.111370, 31.192553, 30.917138,31.324435,27.978620,29.590988,27.188222,27.047695, 25.670264,                 23.960397,31.076449,28.684537,22.580600,22.375813,26.331926,31.281150,28.208842,25.688581,25.558141,                 30.116704,30.095975};         double[] placelongitude = {31.413910, 29.953141,29.693375, 27.222200,34.393354,34.778946 , 33.800840, 31.013473 , 32.704063,                 32.821163,33.832256,34.062882, 28.720754, 31.611667,31.728437,32.242223,33.645257,28.972356,34.582821,                 30.916667,31.362748};          double[][] matrix  = new double[21][21];          googlemap.setmylocationenabled(true);         googlemap.movecamera(cameraupdatefactory.newlatlngzoom(new latlng(placelatitude[0], placelongitude[0]), 13));         for(int = 0 ; i<21;i++)         {             googlemap.addmarker(new markeroptions()                     .snippet(placenamessnippet[i])                     .position(new latlng(placelatitude[i], placelongitude[i]))                     .title(placenames[i]));         }          for(int i=0;i<21;i++)         {             for(int j=0;j<21;j++)             {                 matrix[i][j] = distance(placelatitude[i],placelongitude[i],placelatitude[j],placelongitude[j],'k');             }         }          shortest = tsp.tsp(matrix);           for(int i=0; < 20 ; i++)         {              googlemap.addpolyline(new polylineoptions().geodesic(true)                             .add(new latlng(placelatitude[shortest[i]],placelongitude[shortest[i]]))                             .add(new latlng(placelatitude[shortest[i+1]],placelongitude[shortest[i+1]]))             );         }     }      private void createmapview(){         /**          */         try {             if(null == googlemap){                 googlemap = ((mapfragment) getfragmentmanager().findfragmentbyid(                         r.id.mapview)).getmap();                 /**                  * if map still null after attempted initialisation,                  * show error user                  */                 if(null == googlemap) {                     toast.maketext(getapplicationcontext(),                             "error creating map", toast.length_short).show();                 }             }         } catch (nullpointerexception exception){             log.e("mapapp", exception.tostring());         }     }     private double rad2deg(double rad) {         return (rad * 180 / math.pi);     }     private double deg2rad(double deg) {         return (deg * math.pi / 180.0);     }     private double distance(double lat1, double lon1, double lat2, double lon2, char unit)     {         double theta = lon1 - lon2;         double dist = math.sin(deg2rad(lat1)) * math.sin(deg2rad(lat2)) + math.cos(deg2rad(lat1)) * math.cos(deg2rad(lat2)) * math.cos(deg2rad(theta));         dist = math.acos(dist);         dist = rad2deg(dist);         dist = dist * 60 * 1.1515;         if (unit == 'k') {             dist = dist * 1.609344;         } else if (unit == 'n') {             dist = dist * 0.8684;         }         return (dist);     }     public class tspnearestneighbour     {         private int numberofnodes;         private stack<integer> stack;          public tspnearestneighbour()         {             stack = new stack<integer>();         }          public int[] tsp(double adjacencymatrix[][])         {             numberofnodes = adjacencymatrix[0].length ;             int[] result = new int[adjacencymatrix[0].length];             int resultcounter = 1;             int[] visited = new int[numberofnodes];             visited[0] = 1;             stack.push(0);             int element, dst = 0, i;             double min = double.max_value;             boolean minflag = false;             result[0] = 0;              //system.out.print(1 + "\t");              while (!stack.isempty())             {                 element = stack.peek();                 = 0;                 min = double.max_value;                 while (i < numberofnodes)                 {                     if (adjacencymatrix[element][i] > 1 && visited[i] == 0)                     {                         if (min > adjacencymatrix[element][i])                         {                             min = adjacencymatrix[element][i];                             dst = i;                             minflag = true;                         }                     }                     i++;                 }                 if (minflag)                 {                     visited[dst] = 1;                     stack.push(dst);                     result[resultcounter] = dst;                     resultcounter++;                     //system.out.print(dst + "\t");                     minflag = false;                     continue;                 }                 stack.pop();             }             return result;         }      }  } 

class have buttons

package com.example.gmapsapp;  import android.content.intent; import android.os.bundle; import android.view.view;  public class homepage extends mainactivity {     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.layout1);   }     public void onclick(view view)     {       intent intent = new intent(this, mainactivity.class);         startactivity(intent);     }     public void onclick1(view view)     {       intent intent = new intent(this, airports.class);         startactivity(intent);     }  } 

and logcat is

 03-28 16:19:48.819: e/mapapp(4133): java.lang.nullpointerexception 03-28 16:19:48.829: e/androidruntime(4133): fatal exception: main 03-28 16:19:48.829: e/androidruntime(4133): process: com.example.gmapsapp, pid: 4133 03-28 16:19:48.829: e/androidruntime(4133): java.lang.runtimeexception: unable start activity componentinfo{com.example.gmapsapp/com.example.gmapsapp.airports}: java.lang.nullpointerexception 03-28 16:19:48.829: e/androidruntime(4133):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) 03-28 16:19:48.829: e/androidruntime(4133):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 03-28 16:19:48.829: e/androidruntime(4133):     @ android.app.activitythread.access$800(activitythread.java:135) 03-28 16:19:48.829: e/androidruntime(4133):     @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 03-28 16:19:48.829: e/androidruntime(4133):     @ android.os.handler.dispatchmessage(handler.java:102) 03-28 16:19:48.829: e/androidruntime(4133):     @ android.os.looper.loop(looper.java:136) 03-28 16:19:48.829: e/androidruntime(4133):     @ android.app.activitythread.main(activitythread.java:5017) 03-28 16:19:48.829: e/androidruntime(4133):     @ java.lang.reflect.method.invokenative(native method) 03-28 16:19:48.829: e/androidruntime(4133):     @ java.lang.reflect.method.invoke(method.java:515) 03-28 16:19:48.829: e/androidruntime(4133):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 03-28 16:19:48.829: e/androidruntime(4133):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 03-28 16:19:48.829: e/androidruntime(4133):     @ dalvik.system.nativestart.main(native method) 03-28 16:19:48.829: e/androidruntime(4133): caused by: java.lang.nullpointerexception 03-28 16:19:48.829: e/androidruntime(4133):     @ com.example.gmapsapp.airports.oncreate(airports.java:42) 03-28 16:19:48.829: e/androidruntime(4133):     @ android.app.activity.performcreate(activity.java:5231) 03-28 16:19:48.829: e/androidruntime(4133):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 03-28 16:19:48.829: e/androidruntime(4133):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) 03-28 16:19:48.829: e/androidruntime(4133):     ... 11 more 

sorry bad english

you didnt initialize correctly googlemap object. try initializing before line 41. if see initializing other googlemap object in createmapview instead using 1 used later in oncreate method.so googlemap object null.

you should use this:

 googlemap = ((mapfragment) getfragmentmanager().findfragmentbyid(                 r.id.mapview)).getmap(); 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -