在runOnUiThread中使用非final变量

时间:2012-04-16 18:21:30

标签: android xml multithreading parsing variables

我正在为Android制作天气应用程序,我正在尝试在runOnUiThread中使用我的Thread中的非final变量。但是,我不允许这样做。

我做错了什么?

Beneath是整个活动的代码。该线程正在解析yr.no。

中的xml
public class WeatherActivity extends Activity implements OnClickListener {

ImageView forecastImage;
TextView updateWeatherText;
TextView temperatureText;
TextView cloudinessText;
TextView windspeedText;
TextView precipitationText;

Thread updateWeather = new Thread(new GetWeather());
Thread updateWeatherFromButton = new Thread(new GetWeather());

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    forecastImage = (ImageView) findViewById(R.id.forecastIcon);

    updateWeatherText = (TextView) findViewById(R.id.updatingWeatherText);

    temperatureText = (TextView) findViewById(R.id.temperatureText);
    cloudinessText = (TextView) findViewById(R.id.cloudinessText);
    windspeedText = (TextView) findViewById(R.id.windspeedText);
    precipitationText = (TextView) findViewById(R.id.precipitationText);

    updateWeather.start();

    Button refreshButton = (Button) findViewById(R.id.refreshButton);
    refreshButton.setOnClickListener(this);
}

class GetWeather implements Runnable {

    public void run() {

        try {

            String temp;
            String windspeed;
            String cloudiness;
            String precipitation;


            URL weatherURL = new URL(
                    "http://api.yr.no/weatherapi/locationforecast/1.8/?lat=62.39;lon=17.30");

            XmlPullParserFactory parserFactory = XmlPullParserFactory
                    .newInstance();
            XmlPullParser parser = parserFactory.newPullParser();

            parser.setInput(weatherURL.openStream(), null);

            updateWeatherText.setText("Updating...");
            int parserEvent = parser.getEventType();
            while (parserEvent != XmlPullParser.END_DOCUMENT) {

                switch (parserEvent) {

                case XmlPullParser.TEXT:
                    String tag = parser.getName();

                    if (tag.compareTo("temperature") == 0) {
                        temp = parser.getAttributeValue(
                                null, "value") + " 'C";
                    } else if (tag.compareTo("windSpeed") == 0) {
                        windspeed = parser.getAttributeValue(
                                null, "mps") + " m/s";

                    } else if (tag.compareTo("cloudiness") == 0) {
                        if (Integer.parseInt(parser.getAttributeValue(null,
                                "percent")) >= 0
                                && Integer
                                        .parseInt(parser.getAttributeValue(
                                                null, "percent")) <= 12.5) {
                            cloudiness = "Klart";
                        } else if (Integer.parseInt(parser
                                .getAttributeValue(null, "percent")) > 12.5
                                && Integer
                                        .parseInt(parser.getAttributeValue(
                                                null, "percent")) < 25) {
                            cloudiness = "Mestadels klart";
                        } else if (Integer.parseInt(parser
                                .getAttributeValue(null, "percent")) >= 25
                                && Integer
                                        .parseInt(parser.getAttributeValue(
                                                null, "percent")) < 75) {
                            cloudiness = "Växlande molnighet";
                        } else if (Integer.parseInt(parser
                                .getAttributeValue(null, "percent")) >= 75
                                && Integer
                                        .parseInt(parser.getAttributeValue(
                                                null, "percent")) < 87.5) {
                            cloudiness = "Mestadels mulet";
                        } else if (Integer.parseInt(parser
                                .getAttributeValue(null, "percent")) >= 87.5
                                && Integer
                                        .parseInt(parser.getAttributeValue(
                                                null, "percent")) <= 100) {
                            cloudiness = "Mulet";
                        }

                    } else if (tag.compareTo("precipitation") == 0) {
                        if (Integer.parseInt(parser.getAttributeValue(null, "minvalue")) > 0) forecastImage.setBackgroundResource(R.drawable.rain);
                        precipitation = "Mellan "
                                        + parser.getAttributeValue(null,
                                                "minvalue")
                                        + " mm och "
                                        + parser.getAttributeValue(null,
                                                "maxvalue") + " mm";
                    }
                    break;
                }
                parserEvent = parser.next();

                runOnUiThread(new Runnable() {

                    public void run() {
                        temperatureText.setText(temp);
                        windspeedText.setText(windspeed);
                        cloudinessText.setText(cloudiness);
                        precipitationText.setText(precipitation);

                        if (cloudiness.compareTo("Klart") == 0){
                            forecastImage.setBackgroundResource(R.drawable.sunny);
                        } else if ( cloudiness.compareTo("Mestadels klart" ) == 0) {
                            forecastImage.setBackgroundResource(R.drawable.sunny);
                        }
                          else if ( cloudiness.compareTo("Växlande molnighet") == 0) {
                              forecastImage.setBackgroundResource(R.drawable.cloudy);
                        } else if ( cloudiness.compareTo("Mestadels mulet") == 0) {
                            forecastImage.setBackgroundResource(R.drawable.overcast);
                        } else if ( cloudiness.compareTo("Mulet") == 0) {
                            forecastImage.setBackgroundResource(R.drawable.overcast);
                        }                           
                    }
                });
            }
            updateWeatherText.setText("Last updated: "
                    + System.currentTimeMillis());

        } catch (Exception e) {
        }
    }
}

public void onClick(View v) {

    switch (v.getId()) {

    case R.id.refreshButton:
        updateWeatherFromButton.start();

    }
}

2 个答案:

答案 0 :(得分:1)

有些人已经指出你不能。一种解决方法是在定义Runnable之前将非final变量重新分配给最终变量:

String aNonFinalString = "a";
aNonFinalString = "b";

final String aFinalString = aNonFinalString;

runOnUiThread(new Runnable() {
    public void run() {
        useYourString(aFinalString);
     }
}

答案 1 :(得分:0)

你不能..这是Java你不能在匿名内部类中使用外部变量,除非它被声明为final。 Refer to this question to get more details