我试图在Android中创建一个天气应用程序,我从用户那里得到输入,然后告诉他们当前的天气。主要活动看起来像这样
package com.example.dellinspiron.speechrecognition;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.graphics.Typeface;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView cityField, detailsField, currentTemperatureField, humidity_field, pressure_field, weatherIcon, updatedField;
Typeface weatherFont;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
Button b=(Button)findViewById(R.id.button);
assert b != null;
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText c=(EditText)findViewById(R.id.city);
EditText d=(EditText)findViewById(R.id.country);
final String city=c.getText().toString();
final String country=d.getText().toString();
weatherFont = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/weathericons-regular-webfont.ttf");
cityField = (TextView)findViewById(R.id.city_field);
updatedField = (TextView)findViewById(R.id.updated_field);
detailsField = (TextView)findViewById(R.id.details_field);
currentTemperatureField = (TextView)findViewById(R.id.current_temperature_field);
humidity_field = (TextView)findViewById(R.id.humidity_field);
pressure_field = (TextView)findViewById(R.id.pressure_field);
weatherIcon = (TextView)findViewById(R.id.weather_icon);
weatherIcon.setTypeface(weatherFont);
Function.placeIdTask asyncTask =new Function.placeIdTask(new Function.AsyncResponse() {
public void processFinish(String weather_city, String weather_description, String weather_temperature, String weather_humidity, String weather_pressure, String weather_updatedOn, String weather_iconText, String sun_rise) {
cityField.setText(weather_city);
updatedField.setText(weather_updatedOn);
detailsField.setText(weather_description);
currentTemperatureField.setText(weather_temperature);
humidity_field.setText("Humidity: "+weather_humidity);
pressure_field.setText("Pressure: "+weather_pressure);
weatherIcon.setText(Html.fromHtml(weather_iconText));
}
});
asyncTask.execute(city, country); // having problems here
}
});
}
当我尝试明确传递城市名称和国家时,例如asyncTask.execute(" rawalpindi","巴基斯坦")它可以工作,但是如果我尝试传递一个字符串获得从文本框中它没有。 我试图得到字符串的输出,它给出了正确的输出,但它只是没有正确传递给执行函数。请帮忙。
答案 0 :(得分:0)
当您从EditText获取文本时,尝试使用trim
方法trim()
字符串,因为额外的空格可能会导致结果中的错误。