android.widget.LinearLayout 无法转换为 android.support.v7.widget.RecyclerView。

huangapple go评论73阅读模式
英文:

android.widget.LinearLayout cannot be cast to android.support.v7.widget.RecyclerView

问题

public class FragmentSongs extends Fragment {

    RecyclerView recyclerView;
    ArrayList<ItemSong> arrayList;
    AdapterSongList adapterSongList;
    ZProgressHUD progressHUD;
    GridLayoutManager gridLayoutManager;
    LinearLayoutManager linearLayoutManager;
    TextView textView_empty;
    Button button_try;
    LinearLayout ll_empty;
    String errr_msg;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.layout_songlist, container, false);

        progressHUD = ZProgressHUD.getInstance(getActivity());
        progressHUD.setMessage(getActivity().getResources().getString(R.string.loading));
        progressHUD.setSpinnerType(ZProgressHUD.FADED_ROUND_SPINNER);

        arrayList = new ArrayList<>();
        recyclerView = rootView.findViewById(R.id.ll_songlist);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setHasFixedSize(true);

        ll_empty = rootView.findViewById(R.id.ll_empty);
        textView_empty = rootView.findViewById(R.id.textView_empty_msg);
        button_try = rootView.findViewById(R.id.button_empty_try);

        loadSongs();

        recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                FragmentManager fm = getFragmentManager();
                FragmentSongs f1 = new FragmentSongs();
                FragmentTransaction ft = fm.beginTransaction();

                Bundle bundl = new Bundle();
                bundl.putString("type", getString(R.string.allsong));
                bundl.putString("id", arrayList.get(getPosition(adapterSongList.getID(position))).getId());
                bundl.putString("name", arrayList.get(getPosition(adapterSongList.getID(position))).getMp3Name());
                f1.setArguments(bundl);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                ft.hide(getFragmentManager().findFragmentByTag(getResources().getString(R.string.allsong)));
                ft.add(R.id.fragment, f1, arrayList.get(getPosition(adapterSongList.getID(position))).getMp3Name());
                ft.addToBackStack(arrayList.get(getPosition(adapterSongList.getID(position))).getMp3Name());
                ft.commit();
            }
        }));

        button_try.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadSongs();
            }
        });

        return rootView;
    }

    private void loadSongs() {
        if (JsonUtils.isNetworkAvailable(getActivity())) {
            new LoadArtist().execute(Constant.URL_LATEST);
        } else {
            errr_msg = getString(R.string.internet_not_conn);
            setEmpty();
        }
    }

    private class LoadArtist extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            arrayList.clear();
            ll_empty.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
            progressHUD.show();
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... strings) {
            try {
                String json = JsonUtils.getJSONString(strings[0]);
                JSONObject mainJson = new JSONObject(json);
                JSONArray jsonArray = mainJson.getJSONArray(Constant.TAG_ROOT);
                JSONObject objJson = null;
                for (int i = 0; i < jsonArray.length(); i++) {
                    objJson = jsonArray.getJSONObject(i);

                    String id = objJson.getString(Constant.TAG_ID);
                    String cid = objJson.getString(Constant.TAG_CAT_ID);
                    String cname = objJson.getString(Constant.TAG_CAT_NAME);
                    String artist = objJson.getString(Constant.TAG_ARTIST);
                    String name = objJson.getString(Constant.TAG_SONG_NAME);
                    String url = objJson.getString(Constant.TAG_MP3_URL);
                    String desc = objJson.getString(Constant.TAG_DESC);
                    String duration = objJson.getString(Constant.TAG_DURATION);
                    String total_rate = objJson.getString(Constant.TAG_TOTAL_RATE);
                    String avg_rate = objJson.getString(Constant.TAG_AVG_RATE);
                    String thumb = objJson.getString(Constant.TAG_THUMB_B).replace(" ", "%20");
                    String thumb_small = objJson.getString(Constant.TAG_THUMB_S).replace(" ", "%20");
                    String views = objJson.getString(Constant.TAG_VIEWS);
                    String downloads = objJson.getString(Constant.TAG_DOWNLOADS);

                    ItemSong objItem = new ItemSong(id, cid, cname, artist, url, thumb, thumb_small, name, duration, desc, total_rate, avg_rate,views,downloads);

                    arrayList.add(objItem);
                }
                return "1";
            } catch (JSONException e) {
                e.printStackTrace();
                return "0";
            } catch (Exception ee) {
                ee.printStackTrace();
                return "0";
            }

        }

        @Override
        protected void onPostExecute(String s) {
            if (getActivity() != null) {
                if (s.equals("1")) {
                    progressHUD.dismissWithSuccess(getResources().getString(R.string.success));

                    adapterSongList = new AdapterSongList(getActivity(), arrayList, new RecyclerClickListener() {
                        @Override
                        public void onClick(int position) {
                            if (JsonUtils.isNetworkAvailable(getActivity())) {

                            } else {
                                Toast.makeText(getActivity(), getResources().getString(R.string.internet_not_conn), Toast.LENGTH_SHORT).show();
                            }
                        }
                    }, "online");
                    recyclerView.setAdapter(adapterSongList);
                    errr_msg = getString(R.string.no_data_found);
                } else {
                    progressHUD.dismissWithFailure(getResources().getString(R.string.error));

                    errr_msg = getString(R.string.server_no_conn);
                }

                setEmpty();
                super.onPostExecute(s);
            }
        }
    }

    public void setEmpty() {
        if(arrayList.size() > 0) {
            recyclerView.setVisibility(View.VISIBLE);
            ll_empty.setVisibility(View.GONE);
        } else {
            textView_empty.setText(errr_msg);
            recyclerView.setVisibility(View.GONE);
            ll_empty.setVisibility(View.VISIBLE);
        }
    }

    private int getPosition(String id) {
        int count = 0;
        for (int i = 0; i < arrayList.size(); i++) {
            if (id.equals(arrayList.get(i).getId())) {
                count = i;
                break;
            }
        }
        return count;
    }
}

public class AdapterSongList extends RecyclerView.Adapter<AdapterSongList.MyViewHolder> {

    private Context context;
    private ArrayList<ItemSong> arrayList;
    private ArrayList<ItemSong> filteredArrayList;
    private RecyclerClickListener recyclerClickListener;
    private NameFilter filter;
    private String type;
    private JsonUtils jsonUtils;

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView_song, textView_duration, textView_catname, textView_total_rate, textView_views, textView_downloads;
        EqualizerView equalizer;
        ImageView imageView, imageView_option;
        LinearLayout linearLayout, ll_counts;
        RatingBar ratingBar;

        MyViewHolder(View view) {
            super(view);
            linearLayout = view.findViewById(R.id.ll_songlist);
            ll_counts = view.findViewById(R.id.ll_counts);
            textView_song = view.findViewById(R.id.textView_songname);
            textView_duration = view.findViewById(R.id.textView_songduration);
            textView_total_rate = view.findViewById(R.id.textView_totalrate_songlist);
            equalizer = view.findViewById(R.id.equalizer_view);
            textView_catname = view.findViewById(R.id.textView_catname);
            imageView = view.findViewById(R.id.imageView_songlist);
            imageView_option = view.findViewById(R.id.imageView_option_songlist);
            ratingBar = view.findViewById(R.id.ratingBar_songlist);
            textView_views = view.findViewById(R.id.textView_views);
            textView_downloads = view.findViewById(R.id.textView_downloads

<details>
<summary>英文:</summary>

im doing paid app customization searched and followed some instruction yet couldnt get the job done. copied the same data from other layout and Java classes to make new one as it explains but conflicted with layout
my fragment:
    
        public class FragmentSongs extends Fragment {
    
        RecyclerView recyclerView;
        ArrayList&lt;ItemSong&gt; arrayList;
        AdapterSongList adapterSongList;
        ZProgressHUD progressHUD;
        GridLayoutManager gridLayoutManager;
        LinearLayoutManager linearLayoutManager;
        TextView textView_empty;
        Button button_try;
        LinearLayout ll_empty;
        String errr_msg;
    
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.layout_songlist, container, false);
    
            progressHUD = ZProgressHUD.getInstance(getActivity());
            progressHUD.setMessage(getActivity().getResources().getString(R.string.loading));
            progressHUD.setSpinnerType(ZProgressHUD.FADED_ROUND_SPINNER);
    
            arrayList = new ArrayList&lt;&gt;();
            recyclerView = rootView.findViewById(R.id.ll_songlist);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
            recyclerView.setLayoutManager(gridLayoutManager);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
           recyclerView.setHasFixedSize(true);
    
            ll_empty = rootView.findViewById(R.id.ll_empty);
            textView_empty = rootView.findViewById(R.id.textView_empty_msg);
            button_try = rootView.findViewById(R.id.button_empty_try);
    
            loadSongs();
    
            recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    FragmentManager fm = getFragmentManager();
                    FragmentSongs f1 = new FragmentSongs();
                    FragmentTransaction ft = fm.beginTransaction();
    
                    Bundle bundl = new Bundle();
                    bundl.putString(&quot;type&quot;, getString(R.string.allsong));
                    bundl.putString(&quot;id&quot;, arrayList.get(getPosition(adapterSongList.getID(position))).getId());
                    bundl.putString(&quot;name&quot;, arrayList.get(getPosition(adapterSongList.getID(position))).getMp3Name());
                    f1.setArguments(bundl);
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    ft.hide(getFragmentManager().findFragmentByTag(getResources().getString(R.string.allsong)));
                    ft.add(R.id.fragment, f1, arrayList.get(getPosition(adapterSongList.getID(position))).getMp3Name());
                    ft.addToBackStack(arrayList.get(getPosition(adapterSongList.getID(position))).getMp3Name());
                    ft.commit();
                }
            }));
    
            button_try.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    loadSongs();
                }
            });
    
            return rootView;
        }
    
        private void loadSongs() {
            if (JsonUtils.isNetworkAvailable(getActivity())) {
                new LoadArtist().execute(Constant.URL_LATEST);
            } else {
                errr_msg = getString(R.string.internet_not_conn);
                setEmpty();
            }
        }
    
        private class LoadArtist extends AsyncTask&lt;String, String, String&gt; {
    
            @Override
            protected void onPreExecute() {
                arrayList.clear();
                ll_empty.setVisibility(View.GONE);
                recyclerView.setVisibility(View.VISIBLE);
                progressHUD.show();
                super.onPreExecute();
            }
    
            @Override
            protected String doInBackground(String... strings) {
                try {
                    String json = JsonUtils.getJSONString(strings[0]);
                    JSONObject mainJson = new JSONObject(json);
                    JSONArray jsonArray = mainJson.getJSONArray(Constant.TAG_ROOT);
                    JSONObject objJson = null;
                    for (int i = 0; i &lt; jsonArray.length(); i++) {
                        objJson = jsonArray.getJSONObject(i);
    
                        String id = objJson.getString(Constant.TAG_ID);
                        String cid = objJson.getString(Constant.TAG_CAT_ID);
                        String cname = objJson.getString(Constant.TAG_CAT_NAME);
                        String artist = objJson.getString(Constant.TAG_ARTIST);
                        String name = objJson.getString(Constant.TAG_SONG_NAME);
                        String url = objJson.getString(Constant.TAG_MP3_URL);
                        String desc = objJson.getString(Constant.TAG_DESC);
                        String duration = objJson.getString(Constant.TAG_DURATION);
                        String total_rate = objJson.getString(Constant.TAG_TOTAL_RATE);
                        String avg_rate = objJson.getString(Constant.TAG_AVG_RATE);
                        String thumb = objJson.getString(Constant.TAG_THUMB_B).replace(&quot; &quot;, &quot;%20&quot;);
                        String thumb_small = objJson.getString(Constant.TAG_THUMB_S).replace(&quot; &quot;, &quot;%20&quot;);
                        String views = objJson.getString(Constant.TAG_VIEWS);
                        String downloads = objJson.getString(Constant.TAG_DOWNLOADS);
    
                        ItemSong objItem = new ItemSong(id, cid, cname, artist, url, thumb, thumb_small, name, duration, desc, total_rate, avg_rate,views,downloads);
    
                        arrayList.add(objItem);
                    }
                    return &quot;1&quot;;
                } catch (JSONException e) {
                    e.printStackTrace();
                    return &quot;0&quot;;
                } catch (Exception ee) {
                    ee.printStackTrace();
                    return &quot;0&quot;;
                }
    
            }
    
            @Override
            protected void onPostExecute(String s) {
                if (getActivity() != null) {
                    if (s.equals(&quot;1&quot;)) {
                        progressHUD.dismissWithSuccess(getResources().getString(R.string.success));
    
                        adapterSongList = new AdapterSongList(getActivity(), arrayList, new RecyclerClickListener() {
                            @Override
                            public void onClick(int position) {
                                if (JsonUtils.isNetworkAvailable(getActivity())) {
    
                                } else {
                                    Toast.makeText(getActivity(), getResources().getString(R.string.internet_not_conn), Toast.LENGTH_SHORT).show();
                                }
                            }
                        }, &quot;online&quot;);
                        recyclerView.setAdapter(adapterSongList);
                        errr_msg = getString(R.string.no_data_found);
                    } else {
                        progressHUD.dismissWithFailure(getResources().getString(R.string.error));
    
                        errr_msg = getString(R.string.server_no_conn);
                    }
    
                    setEmpty();
                    super.onPostExecute(s);
                }
            }
        }
    
    
        public void setEmpty() {
            if(arrayList.size() &gt; 0) {
                recyclerView.setVisibility(View.VISIBLE);
                ll_empty.setVisibility(View.GONE);
            } else {
                textView_empty.setText(errr_msg);
                recyclerView.setVisibility(View.GONE);
                ll_empty.setVisibility(View.VISIBLE);
            }
        }
    
        private int getPosition(String id) {
            int count = 0;
            for (int i = 0; i &lt; arrayList.size(); i++) {
                if (id.equals(arrayList.get(i).getId())) {
                    count = i;
                    break;
                }
            }
            return count;
        }
    
and my Adapter
    
        public class AdapterSongList extends RecyclerView.Adapter&lt;AdapterSongList.MyViewHolder&gt; {
    
        private Context context;
        private ArrayList&lt;ItemSong&gt; arrayList;
        private ArrayList&lt;ItemSong&gt; filteredArrayList;
        private RecyclerClickListener recyclerClickListener;
        private NameFilter filter;
        private String type;
        private JsonUtils jsonUtils;
    
        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView textView_song, textView_duration, textView_catname, textView_total_rate, textView_views, textView_downloads;
            EqualizerView equalizer;
            ImageView imageView, imageView_option;
            LinearLayout linearLayout, ll_counts;
            RatingBar ratingBar;
    
            MyViewHolder(View view) {
                super(view);
                linearLayout = view.findViewById(R.id.ll_songlist);
                ll_counts = view.findViewById(R.id.ll_counts);
                textView_song = view.findViewById(R.id.textView_songname);
                textView_duration = view.findViewById(R.id.textView_songduration);
                textView_total_rate = view.findViewById(R.id.textView_totalrate_songlist);
                equalizer = view.findViewById(R.id.equalizer_view);
                textView_catname = view.findViewById(R.id.textView_catname);
                imageView = view.findViewById(R.id.imageView_songlist);
                imageView_option = view.findViewById(R.id.imageView_option_songlist);
                ratingBar = view.findViewById(R.id.ratingBar_songlist);
                textView_views = view.findViewById(R.id.textView_views);
                textView_downloads = view.findViewById(R.id.textView_downloads);
            }
        }
    
        public AdapterSongList(Context context, ArrayList&lt;ItemSong&gt; arrayList, RecyclerClickListener recyclerClickListener, String type) {
            this.arrayList = arrayList;
            this.filteredArrayList = arrayList;
            this.context = context;
            this.type = type;
            this.recyclerClickListener = recyclerClickListener;
            jsonUtils = new JsonUtils(context);
        }
    
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.layout_songlist, parent, false);
    
            return new MyViewHolder(itemView);
        }
    
        @Override
        public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
    
            holder.textView_song.setText(arrayList.get(position).getMp3Name());
            holder.textView_duration.setText(arrayList.get(position).getDuration());
    
            if (!type.equals(&quot;offline&quot;)) {
                holder.textView_total_rate.setText(arrayList.get(position).getTotalRate());
                holder.ratingBar.setRating(Float.parseFloat(arrayList.get(position).getAverageRating()));
                holder.textView_views.setText(jsonUtils.format(Double.parseDouble(arrayList.get(position).getViews())));
                holder.textView_downloads.setText(jsonUtils.format(Double.parseDouble(arrayList.get(position).getDownloads())));
            } else {
                holder.textView_total_rate.setVisibility(View.GONE);
                holder.ratingBar.setVisibility(View.GONE);
                holder.ll_counts.setVisibility(View.GONE);
            }
    
            if (!type.equals(&quot;offline&quot;) || type.equals(&quot;fav&quot;)) {
                Picasso.get()
                        .load(arrayList.get(position).getImageSmall())
                        .into(holder.imageView);
            } else {
                holder.imageView.setImageBitmap(arrayList.get(position).getBitmap());
            }
    
            if (Constant.isPlaying &amp;&amp; Constant.arrayList_play.get(Constant.playPos).getId().equals(arrayList.get(position).getId())) {
                holder.equalizer.animateBars();
                holder.equalizer.setVisibility(View.VISIBLE);
            } else {
                holder.equalizer.stopBars();
                holder.equalizer.setVisibility(View.GONE);
            }
    
            if (arrayList.get(position).getArtist() != null) {
                holder.textView_catname.setText(arrayList.get(position).getArtist());
            } else {
                holder.textView_catname.setText(arrayList.get(position).getArtist());
            }
    
            holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    recyclerClickListener.onClick(holder.getAdapterPosition());
                }
            });
    
            holder.imageView_option.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    openOptionPopUp(holder.imageView_option, holder.getAdapterPosition());
                }
            });
        }
    
        @Override
        public long getItemId(int id) {
            return id;
        }
    
        @Override
        public int getItemCount() {
            return arrayList.size();
        }
    
        public String getID(int pos) {
            return arrayList.get(pos).getId();
        }
    
        private void openOptionPopUp(ImageView imageView, final int pos) {
            PopupMenu popup = new PopupMenu(context, imageView);
            popup.getMenuInflater().inflate(R.menu.popup_song, popup.getMenu());
            if (type.equals(&quot;offline&quot;)) {
                popup.getMenu().findItem(R.id.popup_add_song).setTitle(context.getString(R.string.delete));
            }
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.popup_add_song:
                            if (type.equals(&quot;offline&quot;)) {
                                openDeleteDialog(pos);
                            } else {
                                jsonUtils.openPlaylists(arrayList.get(pos));
                            }
                            break;
                    }
                    return true;
                }
            });
            popup.show();
        }
    
        private void openDeleteDialog(final int pos) {
            AlertDialog.Builder dialog = new AlertDialog.Builder(context);
            dialog.setTitle(context.getString(R.string.delete));
            dialog.setMessage(context.getString(R.string.sure_delete));
            dialog.setPositiveButton(context.getString(R.string.delete), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Boolean isDelete = new File(arrayList.get(pos).getMp3Url()).delete();
                    if (isDelete) {
                        arrayList.remove(pos);
                        notifyItemRemoved(pos);
                        Toast.makeText(context, context.getString(R.string.file_deleted), Toast.LENGTH_SHORT).show();
                    }
                }
            });
            dialog.setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
    
                }
            });
            dialog.show();
        }
    
        public Filter getFilter() {
            if (filter == null) {
                filter = new NameFilter();
            }
            return filter;
        }
    
        private class NameFilter extends Filter {
    
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
    
                constraint = constraint.toString().toLowerCase();
                FilterResults result = new FilterResults();
                if (constraint.toString().length() &gt; 0) {
                    ArrayList&lt;ItemSong&gt; filteredItems = new ArrayList&lt;&gt;();
    
                    for (int i = 0, l = filteredArrayList.size(); i &lt; l; i++) {
                        String nameList = filteredArrayList.get(i).getMp3Name();
                        if (nameList.toLowerCase().contains(constraint))
                            filteredItems.add(filteredArrayList.get(i));
                    }
                    result.count = filteredItems.size();
                    result.values = filteredItems;
                } else {
                    synchronized (this) {
                        result.values = filteredArrayList;
                        result.count = filteredArrayList.size();
                    }
                }
                return result;
            }
    
            @SuppressWarnings(&quot;unchecked&quot;)
            @Override
            protected void publishResults(CharSequence constraint,
                                          FilterResults results) {
    
                arrayList = (ArrayList&lt;ItemSong&gt;) results.values;
                notifyDataSetChanged();
            }
        }
and my XML file lay out which im confused about Recylcer View and Linear Layout
    
    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginBottom=&quot;3dp&quot;
        android:background=&quot;@color/black40&quot;
        android:orientation=&quot;vertical&quot;&gt;
    
        &lt;LinearLayout
            android:id=&quot;@+id/ll_songlist&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:orientation=&quot;horizontal&quot;&gt;
    
            &lt;ImageView
                android:id=&quot;@+id/imageView_songlist&quot;
                android:layout_width=&quot;80dp&quot;
                android:layout_height=&quot;80dp&quot;
                android:layout_marginEnd=&quot;5dp&quot;
                android:scaleType=&quot;centerCrop&quot;/&gt;
    
            &lt;LinearLayout
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_gravity=&quot;center&quot;
                android:layout_weight=&quot;1&quot;
                android:orientation=&quot;vertical&quot;&gt;
    
                &lt;TextView
                    android:id=&quot;@+id/textView_songname&quot;
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:ellipsize=&quot;end&quot;
                    android:maxLines=&quot;1&quot;
                    android:textColor=&quot;@color/white&quot;
                    android:textSize=&quot;15sp&quot;
                    android:layout_marginEnd=&quot;5dp&quot;/&gt;
    
                &lt;TextView
                    android:id=&quot;@+id/textView_catname&quot;
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:ellipsize=&quot;end&quot;
                    android:maxLines=&quot;1&quot;
                    android:text=&quot;sdasd&quot;
                    android:textColor=&quot;@color/white80&quot;
                    android:textSize=&quot;10sp&quot;
                    android:layout_marginBottom=&quot;10dp&quot;/&gt;
    
                &lt;LinearLayout
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:orientation=&quot;horizontal&quot;
                    android:gravity=&quot;center_vertical&quot;&gt;
    
                    &lt;RatingBar
                        android:id=&quot;@+id/ratingBar_songlist&quot;
                        style=&quot;@style/RatingBar_white&quot;
                        android:layout_width=&quot;wrap_content&quot;
                        android:layout_height=&quot;wrap_content&quot;
                        android:max=&quot;5&quot;
                        android:numStars=&quot;5&quot;
                        android:theme=&quot;@style/RatingBar_white&quot;/&gt;
    
                    &lt;TextView
                        android:id=&quot;@+id/textView_totalrate_songlist&quot;
                        android:layout_width=&quot;wrap_content&quot;
                        android:layout_height=&quot;16dp&quot;
                        android:layout_marginStart=&quot;5dp&quot;
                        android:background=&quot;@drawable/bg_round_white&quot;
                        android:paddingEnd=&quot;4dp&quot;
                        android:paddingStart=&quot;4dp&quot;
                        android:text=&quot;32&quot;
                        android:textColor=&quot;@color/black&quot;
                        android:textSize=&quot;10sp&quot;
                        android:gravity=&quot;center&quot;/&gt;
    
                &lt;/LinearLayout&gt;
    
            &lt;/LinearLayout&gt;
    
            &lt;es.claucookie.miniequalizerlibrary.EqualizerView xmlns:custom=&quot;http://schemas.android.com/apk/res-auto&quot;
                android:id=&quot;@+id/equalizer_view&quot;
                android:layout_width=&quot;30dp&quot;
                android:layout_height=&quot;30dp&quot;
                android:layout_gravity=&quot;center&quot;
                android:layout_marginEnd=&quot;7dp&quot;
                android:layout_marginStart=&quot;7dp&quot;
                android:visibility=&quot;invisible&quot;
                custom:animDuration=&quot;3000&quot;
                custom:foregroundColor=&quot;@color/pink&quot; /&gt;
    
            &lt;LinearLayout
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_marginTop=&quot;15dp&quot;
                android:layout_marginEnd=&quot;35dp&quot;
                android:orientation=&quot;vertical&quot;&gt;
    
                &lt;TextView
                    android:id=&quot;@+id/textView_songduration&quot;
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:textColor=&quot;@color/white&quot;
                    android:textSize=&quot;12sp&quot;
                    android:text=&quot;asad&quot;/&gt;
    
            &lt;/LinearLayout&gt;
    
        &lt;/LinearLayout&gt;
    
        &lt;ImageView
            android:id=&quot;@+id/imageView_option_songlist&quot;
            android:layout_width=&quot;30dp&quot;
            android:layout_height=&quot;30dp&quot;
            android:layout_alignParentEnd=&quot;true&quot;
            android:layout_marginEnd=&quot;3dp&quot;
            android:layout_marginTop=&quot;3dp&quot;
            android:background=&quot;@drawable/bar_selector_white&quot;
            android:src=&quot;@mipmap/more_option&quot; /&gt;
    
        &lt;LinearLayout
            android:id=&quot;@+id/ll_counts&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_gravity=&quot;bottom&quot;
            android:layout_marginEnd=&quot;15dp&quot;
            android:orientation=&quot;horizontal&quot;
            android:layout_alignParentEnd=&quot;true&quot;
            android:layout_alignParentBottom=&quot;true&quot;
            android:gravity=&quot;center&quot;&gt;
    
            &lt;ImageView
                android:layout_width=&quot;15dp&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:src=&quot;@mipmap/views&quot; /&gt;
    
            &lt;TextView
                android:id=&quot;@+id/textView_views&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_marginStart=&quot;2dp&quot;
                android:gravity=&quot;center&quot;
                android:text=&quot;25&quot;
                android:textColor=&quot;@color/white&quot;
                android:textSize=&quot;10sp&quot; /&gt;
    
            &lt;ImageView
                android:layout_width=&quot;15dp&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_marginStart=&quot;6dp&quot;
                android:src=&quot;@mipmap/download_arrow&quot; /&gt;
    
            &lt;TextView
                android:id=&quot;@+id/textView_downloads&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_marginStart=&quot;2dp&quot;
                android:gravity=&quot;center&quot;
                android:text=&quot;25&quot;
                android:textColor=&quot;@color/white&quot;
                android:textSize=&quot;10sp&quot; /&gt;
        &lt;/LinearLayout&gt;
    
    &lt;/RelativeLayout&gt;



</details>


# 答案1
**得分**: 0

你在`Fragment``Adapter`中都在使用相同的布局`R.layout.layout_songlist`。

`FragmentSongs``onCreateView`方法中存在问题

```java
recyclerView = rootView.findViewById(R.id.ll_songlist);

因为在布局中你使用了:

&lt;LinearLayout
    android:id=&quot;@+id/ll_songlist&quot;

而你不能将LinearLayout转换为RecyclerView。在你的FragmentSongs中,你应该使用一个带有RecyclerView的不同布局。

英文:

You are using the same layout R.layout.layout_songlist in the Fragment and in the Adapter for each item.

In the onCreateView method of the FragmentSongs the issue is here:

recyclerView = rootView.findViewById(R.id.ll_songlist);

because in the layout you are using:

&lt;LinearLayout
android:id=&quot;@+id/ll_songlist&quot;

and you can't cast a LinearLayout to a RecyclerView.
In your FragmentSongs you should use a different layout with a RecyclerView.

huangapple
  • 本文由 发表于 2020年10月13日 06:25:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64326096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定