[英]How to fix java.lang.ClassCastException… cannot cast interface
In my main activity i have a fragment which displays weather information. 在我的主要活动中,我有一个显示天气信息的片段。 I have an interface between the fragment and the main activity that sends data from fragment to activity.
我在片段和主要活动之间有一个接口,用于将数据从片段发送到活动。 The problem is when trying to specifically enter the fragment activity the app crashes and this error is displayed.
问题是当尝试专门输入片段活动时,应用程序崩溃并显示此错误。
Any help or any alternative ways doing it would be appreciated. 任何帮助或任何其他方式做到这一点将不胜感激。
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)
Your problem is in this line. 你的问题就在这一行。 You are trying casting your
activity
to getFirstWeatherInterface
您正在尝试将您的
activity
为getFirstWeatherInterface
getFirstWeatherInterface getFirstWeather = (getFirstWeatherInterface) getActivity();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.