gpt4 book ai didi

java - 使用 setOnScrollListener 时出现 ClassCastException 错误

转载 作者:行者123 更新时间:2023-12-01 20:05:20 26 4
gpt4 key购买 nike

下面是一个代码,其中数据将在滚动时出现,但有一个错误,即类转换异常。请帮我解决这个错误。现在我已经添加了整个 fragment 类Similar_Matchs_Tab,我们可以在其中从服务器获取。

java.lang.ClassCastException:com.devbhoomimedia.maangal.ProfilesActivity 无法转换为 android.widget.AbsListView$OnScrollListener

public class Similar_Matchs_Tab extends Fragment  {
private ListView listView;
public Similar_Matchs_Tab() {}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
JSON_URL = "https://www.maangal.com/maangal_mobile/similar_matches.php?matri_id="+email;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

膨胀此 fragment 的布局

    View view= inflater.inflate(R.layout.matches_tab, container, false);
listView = (ListView) view.findViewById(R.id.listView);
listView.setOnScrollListener((AbsListView.OnScrollListener) getActivity());
sendRequest();
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {

}

@Override
public void onScroll(AbsListView absListView, int i, int i1, int i2) {

}
});

return view;
}

sendRequest函数来获取数据

private void sendRequest(){
final ProgressDialog loading = ProgressDialog.show(getActivity(),"Loading Data", "Please wait...",false,false);

StringRequest stringRequest = new StringRequest(Request.Method.POST,JSON_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
Log.e("Similar MAtches******",response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(),error.getMessage(), Toast.LENGTH_LONG).show();
}
});
int MY_SOCKET_TIMEOUT_MS = 30000;
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
protected void showJSON(String json){
ParseJSON pj = new ParseJSON(json);
pj.parseJSON();
Profile_Match_custom_List cl = new Profile_Match_custom_List(getActivity(), ParseJSON.ids,ParseJSON.ages, ParseJSON.heights, ParseJSON.communities,ParseJSON.castes,ParseJSON.educations,ParseJSON.occupations,ParseJSON.incomes,ParseJSON.pics,ParseJSON.locations,ParseJSON.shortlist,ParseJSON.expressinterest);
listView.setAdapter(cl);
}
}

最佳答案

因为您正在将 Activity 转换为 AbsListView.OnScrollListener 接口(interface),这就是您收到 classCastException 的原因。

只需删除listView.setOnScrollListener((AbsListView.OnScrollListener) getActivity());

并替换

listView.setOnScrollListener((ProfileActivity) getActivity());

关于java - 使用 setOnScrollListener 时出现 ClassCastException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47509091/

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