0

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();
    }
};
}
sdasdadas
  • 20,387
  • 19
  • 57
  • 135

2 Answers2

2

The code line is "outside" of any method in the class. You can assign its return value to a member variable which would allow it to be on that line, but that is considered "bad form" in programming.

Move the call into a method that your class will call.

daniel
  • 33
  • 2
1

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);   
}
mkso
  • 3,078
  • 2
  • 25
  • 34
  • That worked! But - why? It's in the Entrance class, shouldn't it run? What does isolating it do? – sdasdadas Mar 14 '12 at 23:58
  • just enclosing it with braces does not help. All Java code besides variable declaraions needs to be in a method or a `static` block. – zapl Mar 14 '12 at 23:58
  • http://stackoverflow.com/questions/241088/what-do-curly-braces-by-themselves-mean-in-java Apparently it just limits the scope of the line? – sdasdadas Mar 15 '12 at 00:03
  • 2
    The internet says it's kind of a constructor http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html never seen that before :) – zapl Mar 15 '12 at 00:03