0

I have an activity ShowList where I have some edittext fields and buttons. I have a button "get location" which gets the current location with GPS.

The ShowList activity (which works fine , I can get the location) :

  public class ShowList extends Activity implements OnClickListener{


GPSTracker gps;   //i use GPSTracker to get the location (it is not an activity)
     .........

 case R.id.getlocation:
    // create class object
   gps = new GPSTracker(ShowList.this);

  // check if GPS enabled
 if(gps.canGetLocation()){

          double latitude = gps.getLatitude();
          double longitude = gps.getLongitude();

Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
                    }else{
                        ...
                    }

I have a custom adapter:

    public class myAdapter extends BaseAdapter{   
         ShowList locationclass=new ShowList();
         .....
        TextView Location = (TextView) convertView.findViewById(R.id.text_location);

            String lat=Double.toString(locationclass.gps.getLatitude());
            String lo=Double.toString(locationclass.gps.getLongitude());
            String coordinates="Lat: " + lat + "  Long: " + lo;
            Location.setText(coordinates);

I receive the nullpointer in "String lat...".

So, how can I pass the location from ShowList activity to adapter?

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
George
  • 5,808
  • 15
  • 83
  • 160
  • 1
    You are creating a new object, and I guess the default value for your "gps" inside that class is null. – Carnal Apr 08 '13 at 11:14
  • Do you have this two classes in separate files? Or do the same classes appear in the same file? – Carnal Apr 08 '13 at 11:16
  • @Carnal:Hello, they are in the same project.Actually, I want by using ShowList activity to get the location and pass these values to my adapter in order to use them. – George Apr 08 '13 at 11:18

4 Answers4

2

you can't instanciate an Activity via new. otherwise ,you should pass the context; latitude and longitude to your Adapter in constructor :

public class myAdapter extends BaseAdapter{   
         private String latitude;
         private String longitude;
         private ArrayList<Item> items;
         private Context context; // TODO: use it for layoutInflater

        //the constructor
        public myAdapter(Context context, ArrayList<myItems> list, String latitude, String longitude) {
            this.items = list;
            this.latitude = latitude;
            this.longitude = longitude;
            this.context = context;
        }
         .....
        TextView Location = (TextView) convertView.findViewById(R.id.text_location);
            String coordinates="Lat: " + this.latitude + "  Long: " + this.longitude;
            Location.setText(coordinates);
Houcine
  • 24,001
  • 13
  • 56
  • 83
  • :the problem is that i use public myAdapter(Context context, ArrayList list) { this.context = context; myList = list; } and in other activities.And it shows null for location.Can i avoid this? – George Apr 08 '13 at 11:36
  • just add the latitude and longitude to your constructor of your adapter, see my edits – Houcine Apr 08 '13 at 11:37
  • :yes I did it,The program run, but the values I got were null.I use in MainAcivity myAdapter myAdapterItems = new myAdapter( MainActivity.this, myList); myListView.setAdapter(myAdapterItems); and when I tried : myAdapter myAdapterItems = new myAdapter( MainActivity.this, myList,lat,lo); myListView.setAdapter(myAdapterItems); (i initialized lat,lo) I took null. – George Apr 08 '13 at 11:39
  • @ Houcine:Sorry , I meant I got no values for lat and long.(empty space) – George Apr 08 '13 at 11:40
  • try to display the latitude and longitude in your Activity in Log , before passing them to the adapter's contructor – Houcine Apr 08 '13 at 11:41
  • it means , that the values passed of latitude and longitude are passed empty , as i said , try to dislay them in a Log before passing them just to check if they are not empty and they contains the correct values – Houcine Apr 08 '13 at 11:42
  • @George : the toast that you display in the onClick, is it displaying the correct latitude and longitude ? – Houcine Apr 08 '13 at 11:47
  • so after displaying the toast, try to instanciate your adapter and pass the displayed latitude and longitude to it , and 100% it will display the correct values – Houcine Apr 08 '13 at 11:57
  • :Can you tell how to do this?And in myAdapter should I put myAdapter(Context....,latitude,longitude)? – George Apr 08 '13 at 12:00
  • yes this is it , the code in my answer ( for instantiating adapter) , put it bellow your Toast in your onClick method – Houcine Apr 08 '13 at 12:17
  • :I really can't uderstand how to handle this.I have the myAdapter.java,the ShowList.java (where i have the button to get location) and the MainActivity.java where I have my list view.What to put below Toast and also how to handle in MainActivity the myAdapter ?Thanks! – George Apr 08 '13 at 13:56
  • @ Houcine:I want to pass the data from the ShowList to the myAdapter.java in order to use them as Location.setText(coordinates);But because now I changed the adapter in order to contain latitude and longitude as arguments (as you said),i must pass also the data to the mainActivity (where i have the list view) – George Apr 08 '13 at 14:04
  • please edit your question and add the code of your MainActivity.java, ShowList.java and MyAdapter.java , and then describe the scenario that you want, passing data to an adapter is via Constructor ( you already pass data in the constructor : ArrayList items) – Houcine Apr 08 '13 at 14:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27777/discussion-between-george-and-houcine) – George Apr 08 '13 at 14:44
  • if the list is in your MainActivity.java , then the MyAdapter should be instanciated in MainActivity.java , and before that, you should pass the latitude and longitude from ShowList.java to the MainActivity.java via Intent Extras – Houcine Apr 08 '13 at 14:44
  • @George : i will check the removeItem from list and database if i had some free time tonight and i will send the demo back to you – Houcine Apr 11 '13 at 15:57
1

You should not create instance of activity as

ShowList locationclass=new ShowList();

Instead, call the adapter with parameters you want to pass as

MyAdapter myAdapter = new MyAdaper(latitude, longitude);

And change the constructor of MyAdapter accordingly.

Just a note: Don't start class name with a small letter. Follow proper naming conventions.

Edit: (In reply to the comment of OP)

If you are creating the adapter class's object in your activity ShowList.java, then change the call to:

MyAdapter theAdapter = new MyAdaper(latitude, longitude);

And Change class MyAdapter.java as following:

public class myAdapter extends BaseAdapter{ 
    Strig latitude, longitude;  

    public MyAdapter(String lat, String long)
    {
        this.latitude=lat;
        this.longitude = long;
    }
    TextView Location = (TextView) convertView.findViewById(R.id.text_location);

    String lat=Double.toString(latitude);
    String lo=Double.toString(longitude);
    String coordinates="Lat: " + lat + "  Long: " + lo;
    Location.setText(coordinates);
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • :I am a bit confused right now (sorry I don't have much experience).Can you tell me exactly where and how to implement the myAdapter? (note that is myAdapter theadapter=new myAdapter(..);) – George Apr 08 '13 at 11:26
  • In which class do you call myAdapter theadapter=new myAdapter(..);? – MysticMagicϡ Apr 08 '13 at 11:27
  • I don't call it anywhere.I ask where to use it.I meant that the adapter name I use is myAdapter.java. – George Apr 08 '13 at 11:30
  • :the problem is that i use public myAdapter(Context context, ArrayList list) { this.context = context; myList = list; } and in other activities.And it shows null for location.Can i avoid this? – George Apr 08 '13 at 11:37
  • Are you getting proper values in activity? for location? – MysticMagicϡ Apr 08 '13 at 11:42
  • @ TheReader:The toast gives me the correct values but when I go to the list view it shows empty space for lat and long. – George Apr 08 '13 at 11:56
  • @George from where this adapter is assigned values? for list? – MysticMagicϡ Apr 09 '13 at 06:14
  • :Hello , thank you for the help.I upvoted.I accepted the other because it was a little more detailed and faster.Thanks again – George Apr 09 '13 at 07:04
0

Considering that ShowList is an activity, most likely you implemented method onCreate to do all the initialisation. However when you're creating the class with new ShowList(), your onCreate method is not called, instead a default constructor ShowList is called if one is defined. I suspect you haven't defined it, therefore your gps member variable is null - resulting in your NPE.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
0

Firstly:

We don't create Activity in android like this

ShowList locationclass=new ShowList();

Os creates the Activity Instance via its provided delegate like startActivity and startActivityForResult. You have to use the same instance for carrying your data and pass the data reading from the same reference.

And in your case your adapter need to carry that original activity reference.

Secondly.

GPS location tracking takes couple of seconds to minutes sometimes. And sometime it never gives you location update depending upon open sky is available to your device or not. and depending upon some other environmental factors.

So you should call your adapter to display location only after your activity is notifed by the Location Manager with the location updates. You have to request location updates prior to that though. And once you get the location. Cancel the request for fetching further updates.

You need to google more how GPS works in Android and best practices related to it. Hope it helps

Rohit Sharma
  • 13,787
  • 8
  • 57
  • 72