我在下面放置一个代码,我已经创建了一个本地线程并在最后一个关闭大括号中获得错误,任何人都可以帮我解决一下。
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)
答案 0 :(得分:3)
如果你正确缩进代码,那么这样的问题会更容易被发现。
如果我正确匹配大括号,我认为应该通过简单地删除)
字符来解决直接问题(语法错误)。
但是,匿名线程类实例化中的this
参数确实存在疑问:
this
可能是Runnable
的一个实例。如果是这样,那么覆盖run
Thread
方法会失败通过构造函数传递Runnable
的目的。 Thread
类进行子类化是一个坏主意。我认为你的代码应该是这样的:
Thread dt = new Thread(new Runnable() {
public void run() {
...
}
});
...这可能解释了为什么你在那里有额外的)
!!
如果this
确实是Runnable
你可以写:
Thread dt = new Thread(this);
答案 1 :(得分:1)
LOL ......正如错误所示:删除此令牌,所以这样做:
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();
}
}
};