gpt4 book ai didi

java - 使用 AsyncTask 从互联网更新 Textview

转载 作者:行者123 更新时间:2023-12-02 00:06:52 26 4
gpt4 key购买 nike

我使用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

我怎样才能做到这一点?

最佳答案

盲目猜测是:

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

返回int而不是string。如果它是 int 那么它被认为是资源 Id。这应该有效:

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

关于java - 使用 AsyncTask 从互联网更新 Textview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13671790/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com