-1

I am very new to Android development. So forgive this repetitive question. I've tried many of the solutions on this but none seem to work quite right for me...They all crash on emulator and on my phone.

I'm working on an App and just want to get my GPS Location and then sets the TextView called 'big' to the location's value.

Heres the code. (I have the import statements and everything in my code..I just didnt copy them over

GPSActivity //the activity that starts. the textview R.id.big does exist

public class GPSActivity extends Activity{

private LocationManager       manager = null;
private MyLocationListener    listener;
private TextView             textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    listener = new MyLocationListener(textView);
    textView = (TextView) findViewById(R.id.big);

    manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    listener = new MyLocationListener(textView);

    manager.requestLocationUpdates(LocationManager
            .GPS_PROVIDER, 5000, 10,listener);

    }
} 

I implemented my own LocationListener that changes my TextView...I'm not sure if I'm really allowed to do this....

public class MyLocationListener implements LocationListener
{
TextView textView;

public MyLocationListener(TextView textView)
{
    this.textView = textView;
}

@Override
public void onLocationChanged(Location location) {
    this.textView.setText(location.toString());
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}
}

Lastly my manifest file. I made sure to include the permissions I needed.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mytestapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="12"
    android:targetSdkVersion="16" />
<application

    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar" >
    <activity
        android:name="com.mytestapp.GPSActivity"
        android:permission="ACCESS_FINE_LOCATION"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

The problem occurs in GPSActivity when I call requestLocationUpdates(). The app crashes if I leave this line in the code. If I comment it out, the TextView 'big' displays like I would expect it to.

edit: I've also tried creating a LocationListener as an inner-class like I've seen a lot of other people do on SO...and I had the same result (app crash)

  • 1
    Praytell, what does your logcat say about the crash? – 323go Aug 01 '13 at 22:39
  • Hmmmm.good question I didn't even think to check. On the emulator it says `java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testapp/com.testapp.GPSActivity}: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.` So....What is wrong with my manifest file? –  Aug 01 '13 at 22:42
  • The nice thing about logcat is that it tells you exactly what's wrong in many cases: "location provider requires ACCESS_FINE_LOCATION permission". Add it. – 323go Aug 02 '13 at 13:07
  • Yeah I had the permission line but it was in the wrong place. Now it works perfect. Thanks –  Aug 02 '13 at 15:21

3 Answers3

0

In addition to the ACCESS_FINE_LOCATION permission try adding ACCESS_GPS

and they should look like this ...

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 <uses-permission android:name="android.permission.ACCESS_GPS" />
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
nPn
  • 16,254
  • 9
  • 35
  • 58
0

Add

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

to your manifest.xml file.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
JRomero
  • 4,878
  • 1
  • 27
  • 49
  • Okay...Thanks. My location doesn't show up but at least I'm not crashing. Looks like I have some other problem to figure out. Thanks! –  Aug 01 '13 at 22:53
0

for a complete support of Geolocation using GPS or WiFi use:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>   
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

and for the devices that don´t have support for gps use:

<uses-feature
     android:name="android.hardware.location.gps"
     android:required="false" />
Jorgesys
  • 124,308
  • 23
  • 334
  • 268