简体   繁体   English

android.widget.FrameLayout 不能转换为 androidx.recyclerview.widget.RecyclerView

[英]android.widget.FrameLayout cannot be cast to androidx.recyclerview.widget.RecyclerView

I am doing a swipe refresh function to my recycler view, but it keeps on getting an error that it cannot be cast.我正在对我的回收站视图执行滑动刷新功能,但它不断收到无法投射的错误。

Here is my XML:这是我的 XML:

announcement_recycler_view.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeToRefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/my_announcement_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:paddingBottom="2dp"
            android:paddingTop="2dp"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">


        </androidx.recyclerview.widget.RecyclerView>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</FrameLayout>

AnnouncementFragment公告片段

public class AnnouncementFragment extends Fragment {

    private SharedPrefManager session;
    int status_code = 0;

    //a list to store all the products
    List<Model> announcementList;

    //the recyclerview
    RecyclerView announcementRecyclerView;


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        announcementRecyclerView = (RecyclerView) inflater.inflate(
                R.layout.announcement_recycler_view, container, false);


        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        announcementRecyclerView.setLayoutManager(layoutManager);

        //initializing the productlist
        announcementList = new ArrayList<>();

        // Session class instance
        session = new SharedPrefManager(getActivity());

        loadAnnouncement();

        return announcementRecyclerView;

    }


    public void loadAnnouncement(){

        final Constant WebConfig = new Constant();

        //Call our volley library
        StringRequest stringRequest = new StringRequest(Request.Method.GET, Webconfig.url,
                new Response.Listener < String > () {
                    @Override
                    public void onResponse(String response) {

                        try {
                            JSONObject obj = new JSONObject(response);
                            JSONArray details = obj.getJSONArray("data");

                            if (status_code == 200) {

                                for (int i=0; i<details.length(); i++) {

                                    JSONObject object = details.getJSONObject(i);

                                    announcementList.add(new Model(
                                            object.getInt("id"),
                                            object.getString("date"),
                                            object.getString("title"),
                                            object.getString("details"),
                                            object.getString("photo"),
                                            object.getString("content")
                                    ));

                                }
                                //creating adapter object and setting it to recyclerview
                                AnnouncementAdapter adapter = new AnnouncementAdapter(getActivity(), announcementList);
                                announcementRecyclerView.setAdapter(adapter);



                            } else {

                                Toast.makeText(getActivity(), obj.getString("error"), Toast.LENGTH_SHORT).show();

                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }) {

            @Override
            protected Response < String > parseNetworkResponse(NetworkResponse response) {
                status_code = response.statusCode;
                return super.parseNetworkResponse(response);
            }
        };

        VolleySingleton.getInstance(getActivity()).addToRequestQueue(stringRequest);

    }




}

how can I fix this error?我该如何解决这个错误? I feel that by adding the swipe refresh in my XML, i need to change again my working code on the FragmentActivity , but i don't know how will i do it.我觉得通过在我的 XML 中添加滑动刷新,我需要再次更改FragmentActivity上的工作代码,但我不知道我将如何做。 any help would be really appreciated, I am still new in learning in android thats why I still don't know the proper way of doing this in Fragment .任何帮助将不胜感激,我在 android 中学习仍然是新手,这就是为什么我仍然不知道在Fragment中执行此操作的正确方法。

here is the error logs这是错误日志

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.eplife, PID: 15045
    java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to androidx.recyclerview.widget.RecyclerView
        at com.example.eplife.AnnouncementModule.AnnouncementFragment.onCreateView(AnnouncementFragment.java:48)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
        at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2646)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2416)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2372)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7325)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1321)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1211)

You are inflating a FrameLayout and casting it to RecyclerView , remove your cast and use findViewById to find your RecyclerView .您正在膨胀FrameLayout并将其转换为RecyclerView ,删除您的转换并使用findViewById找到您的RecyclerView

So your code should be like this:所以你的代码应该是这样的:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(
            R.layout.announcement_recycler_view, container, false);
    announcementRecyclerView = view.findViewById(R.id.my_announcement_recycler_view);

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    announcementRecyclerView.setLayoutManager(layoutManager);

    //initializing the productlist
    announcementList = new ArrayList<>();

    // Session class instance
    session = new SharedPrefManager(getActivity());

    loadAnnouncement();

    return view;
}

ClassCastException means that you are casting a component wrongly, ie: you are casting a TextView as an ImageView. ClassCastException意味着您错误地转换组件,即:您将 TextView 转换为 ImageView。

So, make sure that when you are initializing the recyclerView you are not using FrameLayouts ID.因此,请确保在初始化 recyclerView 时您没有使用 FrameLayouts ID。

In your 1st snippets the XML you provided is saying the ID of RecyclerView is my_announcement_recycler_view在您的第一个片段中,您提供的 XML 表示 RecyclerView 的 ID 是my_announcement_recycler_view

But in your class you are initializing your RecyclerView like this但是在您的课堂上,您正在像这样初始化 RecyclerView

 announcementRecyclerView = (RecyclerView) inflater.inflate(
                R.layout.announcement_recycler_view, container, false)

which should be like this应该是这样的

 announcementRecyclerView = (RecyclerView) inflater.inflate(
                R.layout.my_announcement_recycler_view, container, false)

暂无
暂无

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

相关问题 androidx.cardview.widget.CardView 不能转换为 androidx.recyclerview.widget.RecyclerView - androidx.cardview.widget.CardView cannot be cast to androidx.recyclerview.widget.RecyclerView RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView 适配器适配器 - RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView Adapter ADAPTER 这是什么“RecyclerView 没有 LayoutManager androidx.recyclerview.widget.RecyclerView”错误? - What is this "RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView" error? void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager) 在 null object 参考 - void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager) on a null object reference android.widget.FrameLayout $ LayoutParams无法转换为android.widget.AbsListView $ LayoutParams - android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.FrameLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法转换为android.widget.FrameLayout - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout android.widget.FrameLayout无法转换为com.ramotion.foldingcell.FoldingCell - android.widget.FrameLayout cannot be cast to com.ramotion.foldingcell.FoldingCell 尝试从字段 'android.view.View androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView' 读取 null object 参考 - Attempt to read from field 'android.view.View androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView' on a null object reference 尝试从 null object 参考上的字段 'android.view.View androidx.recyclerview.widget.RecyclerView$b0.a' 中读取 - Attempt to read from field 'android.view.View androidx.recyclerview.widget.RecyclerView$b0.a' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM