需要找到线程错误

时间:2010-09-08 04:48:20

标签: java android

我在下面放置一个代码,我已经创建了一个本地线程并在最后一个关闭大括号中获得错误,任何人都可以帮我解决一下。

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) 

2 个答案:

答案 0 :(得分:3)

如果你正确缩进代码,那么这样的问题会更容易被发现。

如果我正确匹配大括号,我认为应该通过简单地删除)字符来解决直接问题(语法错误)。

但是,匿名线程类实例化中的this参数确实存在疑问:

  1. 如果你想传递这样的参数,你需要在你的匿名内部类中使用相应的构造函数。
  2. 看起来this可能是Runnable的一个实例。如果是这样,那么覆盖run Thread方法会失败通过构造函数传递Runnable的目的。
  3. 通常认为对Thread类进行子类化是一个坏主意。
  4. 我认为你的代码应该是这样的:

    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();
        }

    }
};