Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用AsyncTask从internet更新Textview_Java_Android_Android Asynctask_Textview - Fatal编程技术网

Java 使用AsyncTask从internet更新Textview

Java 使用AsyncTask从internet更新Textview,java,android,android-asynctask,textview,Java,Android,Android Asynctask,Textview,我使用AsynTask从网络获取数据并在Textview中显示,这是我的代码 public class PatientAssistant extends Activity { private TextView txtTemp,txtHuminity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

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

    }

}
我怎样才能做到这一点?

盲目猜测是:

txtT.setText(result.getCurrentTempC());
txtH.setText(result.getAtmosphereHumidity());
返回
int
而不是
string
。如果它是
int
,则它被视为资源Id。这将起作用:

txtT.setText( "" + result.getCurrentTempC());
txtH.setText( "" + result.getAtmosphereHumidity());
盲目猜测是:

txtT.setText(result.getCurrentTempC());
txtH.setText(result.getAtmosphereHumidity());
返回
int
而不是
string
。如果它是
int
,则它被视为资源Id。这将起作用:

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

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

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

干杯,你的问题应该是 txthum=txtH 应该是
txtH=txthum

干杯,你的问题应该是 txthum=txtH 应该是 txtH=txthum