0

我使用 AsynTask 从网络获取数据并在 Textview 中显示,这是我的代码

public class PatientAssistant extends Activity {

    private TextView txtTemp,txtHuminity;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.patient_assistant);
        AppPreferences pref=new AppPreferences(this);

        TextView textCity=(TextView)findViewById(R.id.txtCity);

        txtTemp=(TextView)findViewById(R.id.txtTemp);
        txtHuminity=(TextView)findViewById(R.id.txtHumidity);

        textCity.setText(pref.getCity());

        String city=(String) textCity.getText();

        CallWeatherAsync cwa=new CallWeatherAsync(this,txtTemp,txtHuminity);

        cwa.execute(city);


        Button btnSend=(Button)findViewById(R.id.btnSend);
        btnSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //TODO send report

            }
        });
    }

    private class CallWeatherAsync extends AsyncTask<String, Void, WeatherInfo> {

        private PatientAssistant _activity;
        private TextView txtT,txtH;
        public CallWeatherAsync(PatientAssistant activity,TextView txtTmp,TextView txthum)
        {
            _activity=activity;
            txtT=txtTmp;
            txthum=txtH;
        }
        @Override
        protected WeatherInfo doInBackground(String... params) {

            YahooWeatherUtils yahooWeatherUtils = YahooWeatherUtils.getInstance();

            WeatherInfo weatherInfo = yahooWeatherUtils.queryYahooWeather(_activity.getApplicationContext(), params[0]);
            return weatherInfo;
        }
        @Override
        protected void onPostExecute(WeatherInfo result) {

            txtT.setText(result.getCurrentTempC());
            txtH.setText(result.getAtmosphereHumidity());
        }

    }

}

但这会抛出Resources$NotFoundException!!!

12-02 00:39:06.383: E/AndroidRuntime(4035): android.content.res.Resources$NotFoundException: String resource ID #0xc
12-02 00:39:06.383: E/AndroidRuntime(4035):     at android.content.res.Resources.getText(Resources.java:247)
12-02 00:39:06.383: E/AndroidRuntime(4035):     at android.widget.TextView.setText(TextView.java:3473)
12-02 00:39:06.383: E/AndroidRuntime(4035):     at com.assitant.patient.PatientAssistant$CallWeatherAsync.onPostExecute(PatientAssistant.java:116)
12-02 00:39:06.383: E/AndroidRuntime(4035):     at com.assitant.patient.PatientAssistant$CallWeatherAsync.onPostExecute

我怎么能做到这一点?

4

3 回答 3

2

盲目猜测是:

txtT.setText(result.getCurrentTempC());
txtH.setText(result.getAtmosphereHumidity());

返回intstring。如果是,int则将其视为资源 ID。这应该有效:

txtT.setText( "" + result.getCurrentTempC());
txtH.setText( "" + result.getAtmosphereHumidity());
于 2012-12-02T17:36:44.630 回答
1

干杯!,你的问题应该出在 txthum=txtH; 它应该是 txtH =txthum;

于 2012-12-02T18:07:41.727 回答
0

您不要在私有类中设置 TextView。由于您将 2 个 TextView 设置为 PatientAssistant 类的全局变量,因此您应该直接在私有 AsyncClass 中使用它们。

于 2012-12-02T17:39:17.130 回答