簡體   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>)'

我正在構建一個天氣應用程序,如果用戶鍵入一個城市的名稱並單擊一個按鈕,它會顯示不同的內容,例如溫度、日出、日落等。除了圖標之外,一切都運行良好,我無法讓它們工作。 我嘗試使用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM