Thread Error needs to be found
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)
{
开发者_JAVA百科 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)
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:
- If you want to pass an argument like that, you need a corresponding constructor in you anonymous inner class.
- It looks like
this
might be an instance ofRunnable
. If so, then overriding therun
method ofThread
defeats the purpose of passing aRunnable
via the constructor. - 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);
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();
}
}
};
精彩评论