在回收视图应用程序上设置视图适配器

vdgimpew  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(318)

我正在尝试构建一个用于学习如何使用recycleview和适配器的最小应用程序,当我设置适配器时,我的应用程序在没有任何日志记录的情况下崩溃。代码如下:

  1. package com.example.cafe.toplevel;
  2. import android.content.Context;
  3. import android.os.Bundle;
  4. import androidx.fragment.app.Fragment;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import androidx.recyclerview.widget.GridLayoutManager;
  9. import androidx.recyclerview.widget.LinearLayoutManager;
  10. import androidx.recyclerview.widget.RecyclerView;
  11. import com.example.cafe.R;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. public class TopLevelFragment extends Fragment {
  15. private static final String ARG_COLUMN_COUNT = "column-count";
  16. private int mColumnCount = 1;
  17. public TopLevelFragment() {
  18. // Required empty public constructor
  19. }
  20. @SuppressWarnings("unused")
  21. public static TopLevelFragment newInstance(int columnCount) {
  22. TopLevelFragment fragment = new TopLevelFragment();
  23. Bundle args = new Bundle();
  24. args.putInt(ARG_COLUMN_COUNT, columnCount);
  25. return fragment;
  26. }
  27. @Override
  28. public void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. if (getArguments() != null) {
  31. mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
  32. }
  33. }
  34. @Override
  35. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  36. Bundle savedInstanceState) {
  37. // Inflate the layout for this fragment
  38. View view = inflater.inflate(R.layout.fragment_top_level, container, false);
  39. List<CategoryRecyclerViewAdapter.CategoryItem> items = new ArrayList<>();
  40. items.add(new CategoryRecyclerViewAdapter.CategoryItem("1", "Drinks"));
  41. items.add(new CategoryRecyclerViewAdapter.CategoryItem("2", "Food"));
  42. CategoryRecyclerViewAdapter categoryRecyclerViewAdapter = new CategoryRecyclerViewAdapter(items);
  43. RecyclerView recyclerView = view.findViewById(R.id.category_list);
  44. Context context = view.getContext();
  45. if (mColumnCount <= 1) {
  46. recyclerView.setLayoutManager(new LinearLayoutManager(context));
  47. } else {
  48. recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
  49. }
  50. recyclerView.setAdapter(categoryRecyclerViewAdapter);
  51. return view;
  52. }
  53. }

如果您能帮助解决此问题,我们将不胜感激。
编辑:根据请求,我已包含适配器的代码:

  1. package com.example.cafe.toplevel;
  2. import android.view.LayoutInflater;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import android.widget.TextView;
  6. import androidx.recyclerview.widget.RecyclerView;
  7. import com.example.cafe.R;
  8. import java.util.List;
  9. public class CategoryRecyclerViewAdapter extends RecyclerView.Adapter<CategoryRecyclerViewAdapter.ViewHolder> {
  10. public static class CategoryItem {
  11. String name;
  12. String id;
  13. public CategoryItem(String id, String name) {
  14. this.id = id;
  15. this.name = name;
  16. }
  17. }
  18. private final List<CategoryItem> mValues;
  19. public CategoryRecyclerViewAdapter(List<CategoryItem> items) {
  20. mValues = items;
  21. }
  22. @Override
  23. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  24. View view = LayoutInflater.from(parent.getContext())
  25. .inflate(R.layout.fragment_top_level, parent, false);
  26. return new ViewHolder(view);
  27. }
  28. @Override
  29. public void onBindViewHolder(final ViewHolder holder, int position) {
  30. holder.mItem = mValues.get(position);
  31. holder.mIdView.setText(mValues.get(position).id);
  32. holder.mContentView.setText(mValues.get(position).name);
  33. }
  34. @Override
  35. public int getItemCount() {
  36. return mValues.size();
  37. }
  38. public class ViewHolder extends RecyclerView.ViewHolder {
  39. public final View mView;
  40. public final TextView mIdView;
  41. public final TextView mContentView;
  42. public CategoryItem mItem;
  43. public ViewHolder(View view) {
  44. super(view);
  45. mView = view;
  46. mIdView = (TextView) view.findViewById(R.id.category_id);
  47. mContentView = (TextView) view.findViewById(R.id.category_name);
  48. }
  49. @Override
  50. public String toString() {
  51. return super.toString() + " '" + mContentView.getText() + "'";
  52. }
  53. }
  54. }

添加日志错误:

  1. 2021-07-07 22:36:02.916 13387-13387/com.example.cafe E/AndroidRuntime: FATAL EXCEPTION: main
  2. Process: com.example.cafe, PID: 13387
  3. java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
  4. at com.example.cafe.toplevel.CategoryRecyclerViewAdapter.onBindViewHolder(CategoryRecyclerViewAdapter.java:42)
  5. at com.example.cafe.toplevel.CategoryRecyclerViewAdapter.onBindViewHolder(CategoryRecyclerViewAdapter.java:13)
  6. at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7254)
  7. at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7337)
  8. at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6194)
  9. at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6460)
  10. at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
  11. at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
  12. at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
  13. at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
  14. at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
  15. at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
  16. at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4309)
  17. at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4012)
  18. at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4578)
  19. at android.view.View.layout(View.java:22844)
  20. at android.view.ViewGroup.layout(ViewGroup.java:6389)
  21. at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
  22. at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
  23. at android.view.View.layout(View.java:22844)
  24. at android.view.ViewGroup.layout(ViewGroup.java:6389)
  25. at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1855)
  26. at android.view.View.layout(View.java:22844)
  27. at android.view.ViewGroup.layout(ViewGroup.java:6389)
  28. at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
  29. at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
  30. at android.view.View.layout(View.java:22844)
  31. at android.view.ViewGroup.layout(ViewGroup.java:6389)
  32. at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:536)
  33. at android.view.View.layout(View.java:22844)
  34. at android.view.ViewGroup.layout(ViewGroup.java:6389)
  35. at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
  36. at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
  37. at android.view.View.layout(View.java:22844)
  38. at android.view.ViewGroup.layout(ViewGroup.java:6389)
  39. at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
  40. at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
  41. at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
  42. at android.view.View.layout(View.java:22844)
  43. at android.view.ViewGroup.layout(ViewGroup.java:6389)
  44. at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
  45. at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
  46. at com.android.internal.policy.DecorView.onLayout(DecorView.java:784)
  47. at android.view.View.layout(View.java:22844)
  48. at android.view.ViewGroup.layout(ViewGroup.java:6389)
  49. at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3475)
  50. at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2943)
  51. at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1948)
  52. at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8177)
  53. at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
  54. at android.view.Choreographer.doCallbacks(Choreographer.java:796)
  55. at android.view.Choreographer.doFrame(Choreographer.java:731)
  56. 2021-07-07 22:36:02.916 13387-13387/com.example.cafe E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
  57. at android.os.Handler.handleCallback(Handler.java:938)
  58. at android.os.Handler.dispatchMessage(Handler.java:99)
  59. at android.os.Looper.loop(Looper.java:223)
  60. at android.app.ActivityThread.main(ActivityThread.java:7664)
  61. at java.lang.reflect.Method.invoke(Native Method)
  62. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
  63. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
ifmq2ha2

ifmq2ha21#

你的代码不正确。您正在使用充气布局并将其投射到recyclerview。

  1. if (view instanceof RecyclerView) {
  2. Context context = view.getContext();
  3. RecyclerView recyclerView = (RecyclerView) view;
  4. if (mColumnCount <= 1) {
  5. recyclerView.setLayoutManager(new LinearLayoutManager(context));
  6. } else {
  7. recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
  8. }
  9. recyclerView.setAdapter(categoryRecyclerViewAdapter); // commenting this prevents crash but it also doesn't render the list
  10. }

你应该在recyclerview上使用它。 findViewById 回收视图的一部分。然后,您将能够在该recyclerview上设置适配器。
此外,您为列表编写的语法不正确。不要为arraylist保留空泛型。这是不可取的。

  1. List<CategoryRecyclerViewAdapter.CategoryItem> items = new ArrayList<>();

相关问题