Android列表视图不适用于片段

Posted

技术标签:

【中文标题】Android列表视图不适用于片段【英文标题】:Android List view not working with fragment 【发布时间】:2021-10-31 09:31:51 【问题描述】:

我正在尝试使用片段和 Recycler View Adapter 显示图像视图列表。

我每次都会遇到这个异常

  Process: com.example.plasmavietiles, PID: 4860
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean androidx.recyclerview.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference
        at androidx.recyclerview.widget.RecyclerView.findMinMaxChildLayoutPositions(RecyclerView.java:4311)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4045)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3849)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:17637)
        at android.view.ViewGroup.layout(ViewGroup.java:5575)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:17637)
        at android.view.ViewGroup.layout(ViewGroup.java:5575)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
        at android.view.View.layout(View.java:17637)
        at android.view.ViewGroup.layout(ViewGroup.java:5575)
        at com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:148)
        at com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:43)
        at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1892)
        at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:918)
        at android.view.View.layout(View.java:17637)
        at android.view.ViewGroup.layout(ViewGroup.java:5575)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:17637)
        at android.view.ViewGroup.layout(ViewGroup.java:5575)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
        at android.view.View.layout(View.java:17637)
        at android.view.ViewGroup.layout(ViewGroup.java:5575)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:726)
        at android.view.View.layout(View.java:17637)
        at android.view.ViewGroup.layout(ViewGroup.java:5575)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
        at android.view.Choreographer.doCallbacks(Choreographer.java:686)
        at android.view.Choreographer.doFrame(Choreographer.java:621)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

这是我的代码:

问卷调查

public class QuestionnaireActivity  extends Activity implements View.OnClickListener, View.OnTouchListener 


    private ArrayList<Integer> questionList;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_questionnaire);

        /*Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);*/
        prepareView();

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        QuestionFragment question = new QuestionFragment();
        question.setQuestionArray(questionList);
        fragmentTransaction.replace(R.id.questionnaireFragment,question);
        fragmentTransaction.commit();

    

    @Override
    protected void attachBaseContext(Context base)
    
        super.attachBaseContext(LocalHelper.wrap(base));
    


    @SuppressLint("ClickableViewAccessibility")
    private void prepareView()
    

        this.questionList = new ArrayList<>();
        Random r = new Random();
        int tmp;
        for(int i =0 ;i<5;)
            tmp = r.nextInt((20 - 1) + 1) + 1;
            if(!this.questionList.contains(tmp))
            
                this.questionList.add(tmp);
                i++;
            
        

    

问题片段类:

public class QuestionFragment extends Fragment 

    // TODO: Customize parameter argument names
    private static final String ARG_COLUMN_COUNT = "column-count";
    // TODO: Customize parameters
    private int mColumnCount = 1;
    List<Integer> questionArray = new ArrayList<>();

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public QuestionFragment() 

    

    public void setQuestionArray(List<Integer> questionArray) 
        this.questionArray = questionArray;
    

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);



      /*  if (getArguments() != null) 
            mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
        */
    


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        View view = inflater.inflate(R.layout.fragment_question_list, container, false);

        // Set the adapter
        if (view instanceof RecyclerView) 
            Context context = view.getContext();
            RecyclerView recyclerView = (RecyclerView) view;
            if (mColumnCount <= 1) 
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
             else 
                recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
            
            recyclerView.setAdapter(new MyQuestionRecyclerViewAdapter(questionArray));
        
        return view;
    

我的 Recycler 视图适配器类:

public class MyQuestionRecyclerViewAdapter extends RecyclerView.Adapter<MyQuestionRecyclerViewAdapter.ViewHolder> 

    private final List<Integer> mValues;

    public MyQuestionRecyclerViewAdapter(List<Integer> items) 
        mValues = items;
    

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_question, parent, false);
        return new ViewHolder(view);
    

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) 
       // holder.mItem = mValues.get(position);
       // holder.mIdView.setText(mValues.get(position).id);
        holder.mContentView.setImageResource(R.drawable.facebook_icon);
    

    @Override
    public int getItemCount() 
            return mValues.size();
    

    public class ViewHolder extends RecyclerView.ViewHolder 
        public final ImageView mContentView;

        public ViewHolder(View view) 
            super(view);
            //mIdView = (TextView) view.findViewById(R.id.item_number);
            mContentView = (ImageView) view.findViewById(R.id.imageView10);
        
    

【问题讨论】:

【参考方案1】:

你可能想交换这些布局

之前的片段类

View view = inflater.inflate(R.layout.fragment_question_list, container, false);

之前的适配器类

View view = LayoutInflater.from(parent.getContext())
               .inflate(R.layout.fragment_question, parent, false);

之后的片段类

View view = inflater.inflate(R.layout.fragment_question, container, false);

之后的适配器类

View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.fragment_question_l8st, parent, false);

【讨论】:

【参考方案2】:

不要检查view是否为instanceof recyclerview,而是直接通过id查找recyclerview。

RecyclerView recycler_view = view.findViewById(R.id.recyclerViewId);
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) 
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
 else 
    recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));

recyclerView.setAdapter(new MyQuestionRecyclerViewAdapter(questionArray));

【讨论】:

以上是关于Android列表视图不适用于片段的主要内容,如果未能解决你的问题,请参考以下文章

onClick 事件不适用于 android 中的嵌套列表视图项控件

Android FindViewById 仅适用于子布局(列表视图行),不适用于父布局中的 TextView

adjustResize 不适用于片段布局内的 EditText 视图

片段和视图寻呼机

Android,从其他片段返回的空列表视图

水平列表视图不适用于 github