gpt4 book ai didi

java - 如何修复 java.lang.ClassCastException...无法强制转换接口(interface)

转载 作者:行者123 更新时间:2023-12-02 06:08:29 27 4
gpt4 key购买 nike

在我的主要 Activity 中,我有一个显示天气信息的 fragment 。我在 fragment 和主 Activity 之间有一个接口(interface),将数据从 fragment 发送到 Activity 。问题是当尝试专门输入 fragment Activity 时,应用程序崩溃并显示此错误。

任何帮助或任何替代方法将不胜感激。

    private void loadData() {
final List<WeatherForeCast> listWeatherForeCasts = new ArrayList<>();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
"https://www.metaweather.com/api/location/3534/",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("consolidated_weather");
JSONObject jsonObject1 = jsonObject.getJSONObject("parent");
String cityCountry = jsonObject1.getString("title");
cityCountry = cityCountry + " ," + jsonObject.getString("title");
textViewCity.setText(Html.fromHtml(cityCountry ));

for (int x = 0; x < 6; x++) {
JSONObject weatherObject = array.getJSONObject(x);
WeatherForeCast weatherForeCast = new WeatherForeCast(
weatherObject.getInt("air_pressure"),
weatherObject.getInt("wind_speed"),
weatherObject.getInt("the_temp"),
weatherObject.getInt("humidity"),
weatherObject.getString("weather_state_name"),
weatherObject.getString("applicable_date"));
listWeatherForeCasts.add(weatherForeCast);
weatherState = listWeatherForeCasts.get(0).getWeatherState();
if(x == 0) {
sendData();
}
}
;
adapter = new Adapter(listWeatherForeCasts, getActivity());
recyclerView.setAdapter(adapter);

} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);

}




public void sendData()
{
Log.i("fault", weatherState);
getFirstWeatherInterface getFirstWeather = (getFirstWeatherInterface) getActivity();
getFirstWeather.getFirst(weatherState);

}



//Interface is implemented in the Activity
@Override
public WeatherForeCast getFirst(String string) {

Log.i("fault2", string);
return null
}

java.lang.ClassCastException: com.fan4.outdoorplus.WeatherFragments.WeatherActivity cannot be cast to com.fan4.outdoorplus.getFirstWeatherInterface
at com.fan4.outdoorplus.WeatherFragments.WeatherFragment.sendData(WeatherFragment.java:143)
at com.fan4.outdoorplus.WeatherFragments.WeatherFragment$1.onResponse(WeatherFragment.java:117)
at com.fan4.outdoorplus.WeatherFragments.WeatherFragment$1.onResponse(WeatherFragment.java:92)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

最佳答案

你的问题就在这一行。您正在尝试将 activity 转换为 getFirstWeatherInterface

getFirstWeatherInterface getFirstWeather = (getFirstWeatherInterface) getActivity();

关于java - 如何修复 java.lang.ClassCastException...无法强制转换接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55927635/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com