1

I'm trying to build a string with origin and destination gps coordinates for google mapping purposes. The first thing that I need to do is to get the gps coordinates of my current location, since this is the origin point. Then, I need to concatenate these coordinates into a larger string that I use to get directions.

I have code that gets these coordinates, and also code that concatenates them into the correct string format. However, my problem is that my string building code is running first, which is leaving me with null pointer issues since the string is referencing gps coordinates that haven't processed yet.

Here it is. The gotLocation() method comes from implementing advice in this post:

Public class DirectionsActivity extends Activity {

    String myLat, myLng;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Parser parser;

        LocationResult locationResult = new LocationResult(){
            @Override
            public void gotLocation(final Location location){
                try {
                    Double lat = location.getLatitude();
                    Double lng = location.getLongitude();
                    if (lat != 0.0 && lng != 0.0) {                      
                        myLat = Double.toString(lat);
                        myLng = Double.toString(lng);
                        String gps_location = myLat + " " + myLng;
                        Toast.makeText(getBaseContext(), "First Message", Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e) {

                }
            }
        };

        MyLocation myLocation = new MyLocation();
        myLocation.getLocation(this, locationResult);

        Toast.makeText(getBaseContext(), "Second Message", Toast.LENGTH_LONG).show();

        buildString();

        setContentView(R.layout.activity_directions);

    }

The toast output when I run this is "Second Message" followed by "First Message". They should display in the opposite order.

Community
  • 1
  • 1
AndroidDev
  • 20,466
  • 42
  • 148
  • 239
  • You can't have "SecondMessage" shown after "FirstMessage" if you are using code from this question: http://stackoverflow.com/questions/6894195/getting-android-location-and-return-unknown-or-null-when-providers-are-unavail. Just do `buildString()` inside `gotLocation`. – MaciejGórski May 16 '13 at 22:28

1 Answers1

0

Getting a location is an asynchronous operation, you don't have control on the time at which you get the answer from the system.

How would you be sure to find a satellite or cell phone mat before your toast appears ? :)

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • Sure. But is there a way to wait until I do have one? – AndroidDev May 16 '13 at 20:51
  • Sorry. Is there a way to force the program to wait until the gps returns its coordinates. For now, I'm not worried about whether I actually have a connection. I'll build those checks later. I know that I have a connection during my testing. Maybe I could separate these into different threads and force the second thread to wait. Except I have no idea how to do that in Java. Also, I suspect that there may be methods that already enforce the sequencing that I'm interested in. Just a guess. Any ideas? Thanks! – AndroidDev May 16 '13 at 21:17
  • 1
    @usr55410, don't do that. Android may look easy, but it's not. I mean good programming practice and a good understanding of the platform require pretty good programming skill. You should read about the UI (or main) thread in Android. During the hook methods of an activity life cycle (like onCreate) your code is executed on the UI Thread. This thread should never be blocked or freezed as it is the thread that repaints your app and receives user input. If you freeze it, you get at best bad scores on the store and and a bad UX, at worst, an ANR. Re-think your program so that you the data async. – Snicolas May 16 '13 at 21:43
  • I never said that Android was easy. But I am looking for suggestions as to how to solve my problem. – AndroidDev May 16 '13 at 22:10