简体   繁体   English

无法解析方法&#39;with(匿名 com.android.volley.Response.Listener<java.lang.String> )&#39;

[英]Cannot resolve method 'with(anonymous com.android.volley.Response.Listener<java.lang.String>)'

I'm building a weather application where if the user types the name of a city and clicks a button, it displays different things like the temperature, sunrise, sunset, etc. Everything is working fine except for the icons, I cannot get them to work.我正在构建一个天气应用程序,如果用户键入一个城市的名称并单击一个按钮,它会显示不同的内容,例如温度、日出、日落等。除了图标之外,一切都运行良好,我无法让它们工作。 I tried using the Picasso and Glide library but none of them seem to work Here's the code:我尝试使用 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;
}

The error [Cannot resolve method 'with(anonymous com.android.volley.Response.Listener<java.lang.String>)'] that I'm getting is in this part of the code: Glide.with(this).load(url).into(imageView);我得到的错误[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);

Also here's the code that i've tried with Picasso:这也是我用毕加索尝试过的代码:

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);

The Picasso code doesn't throw any errors but as soon as I click the button for the data to show up, the application just closes. Picasso 代码不会引发任何错误,但只要我单击要显示数据的按钮,应用程序就会关闭。

Any help would be greatly Appreciated.任何帮助将不胜感激。 Thank you谢谢

Perhaps you're missing imageView = findViewById(R.id.imageView);也许您缺少imageView = findViewById(R.id.imageView); ? ? It's probably because imageView is null.这可能是因为imageView为空。

将 Gilde.with(this) 替换为 Glide.with(MainActivity.this)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法解析方法&#39;super(?,java.lang.String,com.android.volley.Response.Listener <java.lang.String> ,空值)&#39; - Cannot resolve method 'super(?,java.lang.String, com.android.volley.Response.Listener<java.lang.String>,null)' 无法解析方法&#39;addToRequestQueque(com.android.volley.toolbox.StringRequest,java.lang.String)&#39; - cannot resolve method 'addToRequestQueque(com.android.volley.toolbox.StringRequest,java.lang.String)' 无法解析方法'setTitle(java.lang.String)' android studio - Cannot resolve method 'setTitle(java.lang.String)' android studio Android:无法解析方法“ query(java.lang.string)” - Android: cannot resolve method “query(java.lang.string)” How to solve this error “cannot resolve method 'addData(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)'”? - How to solve this error “cannot resolve method 'addData(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)'”? 无法解析方法创建&#39;com.google.common.net.MediaType,java.lang.String) - cannot resolve method create 'com.google.common.net.MediaType,java.lang.String) 无法解析方法“ List.of(java.lang.String,java.lang.String)” - Cannot resolve method 'List.of(java.lang.String, java.lang.String)' 无法解析方法put(java.lang.string,java.lang.string) - Cannot resolve method put(java.lang.string, java.lang.string) 无法解析方法setText(java.lang.String) - Cannot resolve method setText(java.lang.String) 无法解析片段中的方法setText(java.lang.String) - Cannot resolve method setText(java.lang.String) in Fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM