0

I have been trying to print my lang and long coordinates for the past two days, I have chopped and changed code in order to make it work, in a bit of a muddle now and don't understand why it won't work, I am new to Android Development whilst consulting a book (Beginning Android Development) but it doesn't touch on this subject, any help would be greatly appreciated.

It compiles and I don't get any errors, however it does not print out to the EditText boxes in the application, defined in main.xml.

Here is my code for the Java file:

       package com.emergency;

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Context;
import android.location.LocationManager;
import android.location.Criteria;

        public class EmergencyLocation extends Activity implements LocationListener {
            private TextView latitudeField;
            private TextView longitudeField;
            private LocationManager locationManager;
            private String provider;

            /** Called when the activity is first created. **/
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                latitudeField = (TextView) findViewById(R.id.long_lat1);
                longitudeField = (TextView) findViewById(R.id.long_lat2);

                // Get the location manager
                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                // Define the criteria how to select the location provider -> use
                // default
                Criteria criteria = new Criteria();
                provider = locationManager.getBestProvider(criteria, false);
                locationManager.requestLocationUpdates(provider, 0, 0, this);
                Location location = locationManager.getLastKnownLocation(provider);
                onLocationChanged(location);
            }

            @Override
            protected void onDestroy() {
                super.onDestroy();
                locationManager.removeUpdates(this);
            }

            @Override
            public void onLocationChanged(Location location) {
               if (location != null) {
                   System.out.println("Provider " + provider + " has been selected.");
                   int lat = (int) (location.getLatitude());
                   int lng = (int) (location.getLongitude());
                   latitudeField.setText(String.valueOf(lat));
                   longitudeField.setText(String.valueOf(lng));
                   /*Toast.makeText(EmergencyLocation.this, 
                           "Longitude " + longitudeField + "Latitude " + latitudeField, Toast.LENGTH_LONG).show();*/



                   //getting longitude to display in an EditText box  
                   /*EditText lngtude = (EditText) findViewById(R.id.longitudeField);
                     lngtude.setText(String.valueOf(lng));

                   EditText lattude = (EditText) findViewById(R.id.latitudeField);   
                     lattude.setText(String.valueOf(lat)); */

                   EditText lngtude = (EditText) findViewById(R.id.longitudeField);
                   lngtude.setText(String.valueOf(lng), TextView.BufferType.EDITABLE);

                   EditText lattude = (EditText) findViewById(R.id.latitudeField);   
                   lattude.setText(String.valueOf(lat), TextView.BufferType.EDITABLE);



               } else {
                   latitudeField.setText("Provider not available");
                   longitudeField.setText("Provider not available");
               }
            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStatusChanged(String provider, int status,
                    Bundle extras) {
                // TODO Auto-generated method stub

            }


            /* @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);

                    Button simpleBtn = (Button) findViewById(R.id.location);
                    simpleBtn.setOnClickListener(new View.OnClickListener() {*/


                 }
                ;
PurpleSmurph
  • 2,055
  • 3
  • 32
  • 52
  • 1
    what does it mean, it didn't work? Are you not getting expected values (or) you are getting any errors? – kosa Jan 03 '12 at 17:03
  • It doesn't print out to the defined EditText boxes in main.xml. No errors, it compiles. – PurpleSmurph Jan 03 '12 at 17:06
  • Have you done any debugging at all??? Use Log.d(...) to write to the log, not System.out.println. – muratgu Jan 03 '12 at 17:13
  • Debugging I was told was done when you press F11 within Eclipse before it launches the emulator. Literally "Log.d("prov etc)" instead of "System.out.println" ? – PurpleSmurph Jan 03 '12 at 17:17

2 Answers2

0

I don't believe you're using EditText.setText correctly. See this post as well.

EditText lngtude = (EditText) findViewById(R.id.longitudeField);
lngtude.setText(String.valueOf(lng), TextView.BufferType.EDITABLE);

EditText lattude = (EditText) findViewById(R.id.latitudeField);   
lattude.setText(String.valueOf(lat), TextView.BufferType.EDITABLE);
Community
  • 1
  • 1
Marvin Pinto
  • 30,138
  • 7
  • 37
  • 54
  • Thank you, butI changed the two parts (the above)but it still won't print out to the EditText boxes. – PurpleSmurph Jan 03 '12 at 17:24
  • I don't understand what you mean by "it still won't print out the EditText boxes". Are your Lat/Lon values non null? (as in are you able to print them out to logcat?) Are your EditText boxes being displayed on the UI? (presumably blank) – Marvin Pinto Jan 03 '12 at 17:28
  • Yes, the EditText boxes are blank, I am not going to deny I am possibly being stupid but I really am lost here, thank you for helping. What is LogCat and how do I view it to check? – PurpleSmurph Jan 03 '12 at 17:33
  • @UserSmurph Let's start from the beginning. Are you getting your **latitude** and **longitude** values correctly? – Marvin Pinto Jan 03 '12 at 17:55
  • I am not getting any values displayed in the EditText boxes, even when sending long/lat from the DDMS. – PurpleSmurph Jan 03 '12 at 17:58
  • @UserSmurph Forget the EditText boxes for now. Can you first of all print whatever lat/lon values to either System.out or logcat? This matters because if you're not getting your lat/lon values in the first place, EditText won't display anything. – Marvin Pinto Jan 03 '12 at 18:01
  • When I click "send" in the DDMS location section nothing comes up in LogCat. – PurpleSmurph Jan 03 '12 at 18:05
  • Okay, so you haven't figured out how to get and print out coordinates yet. That's okay. Follow the advice that was given to you in your other question and I would recommend going through a few more tutorials. http://stackoverflow.com/questions/8711075/print-longitude-and-latitude-in-textbox-android – Marvin Pinto Jan 03 '12 at 18:10
  • Thanks for your help, I've been going through tutorials and added more code to try and get it to find the location, but now it won't even compile. – PurpleSmurph Jan 03 '12 at 18:21
  • @UserSmurph If you're still stuck on getting a user's location, see my other answer here http://stackoverflow.com/a/8728299/1101070 – Marvin Pinto Jan 04 '12 at 18:36
  • Got it working in the end thanks, it was the emulator being atrocious, it did work on a phone, thank you anyway. – PurpleSmurph Jan 06 '12 at 12:20
0

You shouldn't call onLocationChanged() from code. That method runs when the location actually changes. On the emulator you have to simulate a location change, either by sending coordinates from the DDMS perspective or by using the geo fix command in a telnet session.

NickT
  • 23,844
  • 11
  • 78
  • 121
  • Oh, not heard of this before! It's not printing "no service" either though,could it still be the same issue? Is there a way to call from current location for the emulation purposes? If not, how would I use the DDMS? – PurpleSmurph Jan 03 '12 at 17:30
  • With the mouse click Window, Open Perspective, DDMS. You may need to do Window, Open Perspective, Other, DDMS. In the DDMS perspective there should be an 'Emulator Control' tab, at the bottom of that tab there is a Location Control area where you can enter a latitude and longitude, then send it to the emulator – NickT Jan 03 '12 at 17:36
  • OK, I did all of that but still nothing came up in the EditText boxes, any ideas? – PurpleSmurph Jan 03 '12 at 17:45
  • Put a break point on onLocationChanged() and start the debugger, or use Logcat – NickT Jan 03 '12 at 17:48
  • The debugger is apparently running all the time, not sure whether that's true but when I hover over it that's what it says. What is a 'break point' ? – PurpleSmurph Jan 03 '12 at 17:50
  • Use Eclipse's Help option and search for 'debugger' or 'breakpoint'. This is basic stuff you need to get a grip on in a 'Hello World' type program before trying anything fancy. – NickT Jan 03 '12 at 18:15
  • The debugger I am familiar with, just hadn't heard of or used 'breakpoint'. Everything was compiling without error, nothing was getting sent to LogCat, trying to fix that, but in doing so I now have a screen of red errors. – PurpleSmurph Jan 03 '12 at 18:41