[英]Cannot resolve method 'super(?,java.lang.String, com.android.volley.Response.Listener<java.lang.String>,null)'
[英]Cannot resolve method 'with(anonymous com.android.volley.Response.Listener<java.lang.String>)'
我正在构建一个天气应用程序,如果用户键入一个城市的名称并单击一个按钮,它会显示不同的内容,例如温度、日出、日落等。除了图标之外,一切都运行良好,我无法让它们工作。 我尝试使用 Picasso 和 Glide 库,但它们似乎都不起作用这是代码:
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
ImageView imageView;
TextView country_tx, city_tx, temp_tx, latitude_tx, longitude_tx, sunrise_tx, humidity_tx, sunset_tx, pressure_tx, windSpeed_tx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.EditText);
button = findViewById(R.id.button);
country_tx = findViewById(R.id.country);
city_tx = findViewById(R.id.city);
temp_tx = findViewById(R.id.temperature);
latitude_tx = findViewById(R.id.latitude);
longitude_tx = findViewById(R.id.longitude);
sunrise_tx = findViewById(R.id.sunrise);
sunset_tx = findViewById(R.id.sunset);
humidity_tx = findViewById(R.id.humidity);
pressure_tx = findViewById(R.id.pressure);
windSpeed_tx = findViewById(R.id.windSpeed);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
findWeather();
}
});
}
public void findWeather() {
String city = editText.getText().toString();
String url = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=f30e42aed61593e2c954e35d72cf9e77&units=metric";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Thirja e API
try {
JSONObject jsonObject = new JSONObject(response);
//Find the State
JSONObject object1 = jsonObject.getJSONObject("sys");
String country_find = object1.getString("country");
country_tx.setText(country_find);
//Find the city
String city_find = jsonObject.getString("name");
city_tx.setText(city_find);
//Find the temperature
JSONObject object2 = jsonObject.getJSONObject("main");
String temp_find = object2.getString("temp");
temp_tx.setText(temp_find + "°C");
//Finding the icon
JSONArray jsonArray = jsonObject.getJSONArray("weather");
JSONObject obj = jsonArray.getJSONObject(0);
String icon = obj.getString("icon");
String url = "https://openweathermap.org/img/wn/"+icon+"@2x.png";
Glide.with(this).load(url).into(imageView);
// Finding the latitude
JSONObject object3 = jsonObject.getJSONObject("coord");
double lat_find = object3.getDouble("lat");
latitude_tx.setText(lat_find + "° N");
//Finding the longitude
JSONObject object4 = jsonObject.getJSONObject("coord");
double lon_find = object4.getDouble("lon");
longitude_tx.setText(lon_find + "° E");
//finding the sunrise
JSONObject object5 = jsonObject.getJSONObject("sys");
long sunrise_find = Long.parseLong(object5.getString("sunrise"));
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String date = sdf.format(new java.util.Date (sunrise_find*1000));
sunrise_tx.setText(date);
//Finding the sunset
JSONObject object6 = jsonObject.getJSONObject("sys");
long sunset_find = Long.parseLong(object6.getString("sunset"));
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
String date2 = sdf2.format(new java.util.Date (sunset_find*1000));
sunset_tx.setText(date2);
//Finding the humidity
JSONObject object7 = jsonObject.getJSONObject("main");
int humidity_find = object7.getInt("humidity");
humidity_tx.setText(humidity_find + " %");
//Finding the pressure
JSONObject object8 = jsonObject.getJSONObject("main");
String pressure_find = object8.getString("pressure");
pressure_tx.setText(pressure_find + " hPa");
//Finding the wind speed
JSONObject object9 = jsonObject.getJSONObject("wind");
String windSpeed_find = object9.getString("speed");
windSpeed_tx.setText(windSpeed_find + " km/h");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
我得到的错误[Cannot resolve method 'with(anonymous com.android.volley.Response.Listener<java.lang.String>)']
在这部分代码中: Glide.with(this).load (url).into(imageView); :
//Finding the icon
JSONArray jsonArray = jsonObject.getJSONArray("weather");
JSONObject obj = jsonArray.getJSONObject(0);
String icon = obj.getString("icon");
String url = "https://openweathermap.org/img/wn/"+icon+"@2x.png";
Glide.with(this).load(url).into(imageView);
这也是我用毕加索尝试过的代码:
JSONArray jsonArray = jsonObject.getJSONArray("weather");
JSONObject obj = jsonArray.getJSONObject(0);
String icon = obj.getString("icon");
String url = "https://openweathermap.org/img/wn/"+icon+"@2x.png";
Picasso.get().load(url).into(imageView);
Picasso 代码不会引发任何错误,但只要我单击要显示数据的按钮,应用程序就会关闭。
任何帮助将不胜感激。 谢谢
也许您缺少imageView = findViewById(R.id.imageView); ? 这可能是因为imageView为空。
将 Gilde.with(this) 替换为 Glide.with(MainActivity.this)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.