Search code examples
javaandroidgpsidentifier

Android - requestLocationUpdates causes identifier expected error


EDIT: Please note that while I received an answer, I don't understand what's wrong or why the solution fixes it. I'm still looking for a debunk...

I am receiving the error:

error: (identifier) expected

on the line

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

where the identifier expected points to the "(".

[Note: I actually get four of these errors pointing to the "(", "0", "0", and the ")".]

I always get this error when I slip up with syntax - but I cannot find it this time. My code is relatively short so I pase it, but please know that the code works completely fine when I comment out the offending line above.

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View.*;
import android.view.View;
import android.content.Context;
import java.lang.CharSequence;
import android.widget.Toast;
import android.location.*;


public class Entrance extends Activity
{

/** Called when the activity is first created. */
@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button bGPS = (Button)findViewById(R.id.button_gps);
        bGPS.setOnClickListener(bGPSListener);
    }

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

LocationListener locationListener = new LocationListener()
{
    public void onLocationChanged(Location location)
    {
        Context context = getApplicationContext();
        CharSequence text = "Location found.";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
};

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);   

private OnClickListener bGPSListener = new OnClickListener()
{
    public void onClick(View v)
    {
        Context context = getApplicationContext();
        CharSequence text = "Hello toast!";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }
};
}

Solution

  • you can't just envoke method any there inside class atleast put it is seperate section like this

    {
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);   
    }