tags:

views:

39

answers:

2

I am placing a code below, I have created a local Thread and getting Error at the last Closing Braces, Can anybody please sort it out for me.

Thread dt = new Thread(this){
public void run() 
    {
        Looper.prepare();
        GetCurrentLocation loc = new GetCurrentLocation(RestaurantFinder.this);
        loc.setLocParams();

        int counter = 0;
        String lat = GetCurrentLocation.getCurrentLatitude();
        String lon = GetCurrentLocation.getCurrentLongitude();

        while (lat == null && lon == null
                && counter <= 1000) 
        {
            lat = GetCurrentLocation.getCurrentLatitude();
            lon = GetCurrentLocation.getCurrentLongitude();
            counter = counter + 1;
        }

        System.out.println("The Latitude are:" + lat);
        System.out.println("The Longitude are:"+ lon);

        if (lat == null && lon == null) 
    {
            // another alert for location not found

            AlertDialog.Builder builder1 = new AlertDialog.Builder(RestaurantFinder.this);
            builder1.setTitle("Restaurant Finder");
            builder1.setMessage("Unable to find the Current Location");
            builder1.setPositiveButton("OK",new DialogInterface.OnClickListener()
            {

                                @Override
                                public void onClick(DialogInterface dialog,int which) 
                                {


                                    dialog.dismiss();

                                }
            });

            AlertDialog dialog1 = builder1.create();
            dialog1.show();

            // dismiss ProgressDialog by Handler
            pd.dismiss();

        } 
        else
        {

            weather();
            // dismiss ProgressDialog by Handler
            pd.dismiss();
        }

    }

});<--(Error:Syntax error on token ")", Delete this token) 
+1  A: 

LOL... as the error says: Delete this token) , so do it this way:

Thread dt = new Thread(this){
public void run() 
    {
        Looper.prepare();
        GetCurrentLocation loc = new GetCurrentLocation(RestaurantFinder.this);
        loc.setLocParams();

        int counter = 0;
        String lat = GetCurrentLocation.getCurrentLatitude();
        String lon = GetCurrentLocation.getCurrentLongitude();

        while (lat == null && lon == null
                && counter <= 1000) 
        {
            lat = GetCurrentLocation.getCurrentLatitude();
            lon = GetCurrentLocation.getCurrentLongitude();
            counter = counter + 1;
        }

        System.out.println("The Latitude are:" + lat);
        System.out.println("The Longitude are:"+ lon);

        if (lat == null && lon == null) 
    {
            // another alert for location not found

            AlertDialog.Builder builder1 = new AlertDialog.Builder(RestaurantFinder.this);
            builder1.setTitle("Restaurant Finder");
            builder1.setMessage("Unable to find the Current Location");
            builder1.setPositiveButton("OK",new DialogInterface.OnClickListener()
            {

                                @Override
                                public void onClick(DialogInterface dialog,int which) 
                                {


                                    dialog.dismiss();

                                }
            });

            AlertDialog dialog1 = builder1.create();
            dialog1.show();

            // dismiss ProgressDialog by Handler
            pd.dismiss();

        } 
        else
        {

            weather();
            // dismiss ProgressDialog by Handler
            pd.dismiss();
        }

    }
};
Cristian
+3  A: 

If you indented your code properly, problems like this would be much easier to spot.

If I've matched the braces correctly, I think that the immediate problem (the syntax error) should be fixed by simply deleting the ) character.

However, there is something really suspicious about the this argument in the anonymous thread class instantiation:

  1. If you want to pass an argument like that, you need a corresponding constructor in you anonymous inner class.
  2. It looks like this might be an instance of Runnable. If so, then overriding the run method of Thread defeats the purpose of passing a Runnable via the constructor.
  3. Subclassing the Thread class is generally thought to be a bad idea.

I think your code ought to look like this:

Thread dt = new Thread(new Runnable() {
     public void run() {
         ...
     }
});

... which might explain why you had the extra ) in there in the first place!!

Or if this really is a Runnable you could write:

Thread dt = new Thread(this);
Stephen C
Thank you so much Stephen C, I sorted it out by referring your answer. It was that "this" which was putting me in slough. It is all correct now.
david