尝试在空对象引用上调用虚拟方法“void androidx.recyclerview.widget.RecyclerView.setLayoutManager()”

Posted

技术标签:

【中文标题】尝试在空对象引用上调用虚拟方法“void androidx.recyclerview.widget.RecyclerView.setLayoutManager()”【英文标题】:Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager()' on a null object reference 【发布时间】:2021-08-30 12:17:46 【问题描述】:

我有片段,我正在使用 recyclerView,但是当我运行应用程序时出现错误“尝试调用虚拟方法'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)'在空对象引用上”

公共类 IncomeFragment 扩展了 Fragment

private RecyclerView recyclerView;
private FirebaseRecyclerAdapter adapter;
private View v;

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

    // Inflate the layout for this fragment
    v = inflater.inflate(R.layout.fragment_expense, container, false);
    recyclerView = (RecyclerView) v.findViewById(R.id.recycler);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setHasFixedSize(true);

    FirebaseAuth mAuth = FirebaseAuth.getInstance();
    FirebaseUser user = mAuth.getCurrentUser();
    assert user != null;
    String uid = user.getUid();
    Query query = FirebaseDatabase.getInstance().getReference().child("IncomeDatabase").child(uid);
    FirebaseRecyclerOptions<Data> options = new FirebaseRecyclerOptions.Builder<Data>()
            .setQuery(query, new SnapshotParser<Data>() 
                @NonNull
                @NotNull
                @Override
                public Data parseSnapshot(@NonNull @NotNull DataSnapshot snapshot) 
                    return new Data(Integer.parseInt(snapshot.child("amount").getValue().toString())
                            , snapshot.child("type").getValue().toString()
                            , snapshot.child("note").getValue().toString()
                            , snapshot.child("id").getValue().toString(),
                            snapshot.child("date").getValue().toString());
                
            ).build();
    adapter = new FirebaseRecyclerAdapter<Data, ViewHolder>(options) 
        @NonNull
        @NotNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) 
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.income_recycler_data, parent, false);
            return new ViewHolder(v);
        

        @Override
        protected void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position, @NonNull @NotNull Data model) 
            holder.setAmount(String.valueOf(model.getAmount()));
            holder.setDate(model.getDate());
            holder.setNote(model.getNote());
            holder.setType(model.getType());

        
    ;
    recyclerView.setAdapter(adapter);
    return v;

【问题讨论】:

【参考方案1】:

看起来它无法在 xml 布局中按 id 找到回收站视图。你确定你的 xml 布局中有 RecyclerView,id=recycler(R.layout.fragment_expense - 布局 id 正确吗?)?

也许您只是使用了不同的布局。我的意思是 收入片段的 R.layout.fragment_费用

【讨论】:

检查这一行: v = inflater.inflate(R.layout.fragment_expense, container, false);收入片段的布局是否正确?

以上是关于尝试在空对象引用上调用虚拟方法“void androidx.recyclerview.widget.RecyclerView.setLayoutManager()”的主要内容,如果未能解决你的问题,请参考以下文章

尝试在空对象引用上调用虚拟方法 com.google.firebase.iid.FirebaseInstanceId.getInstanceId()'

NullPointerException 错误尝试在空对象引用上调用虚拟方法错误

Android错误尝试在空对象引用上调用虚拟方法[重复]

AsyncTask +数据库尝试在空对象引用上调用虚拟方法[重复]

数据库错误:尝试在空对象引用上调用虚拟方法“Cursor .getScene()”[重复]

尝试在空对象引用上调用虚拟方法 'android.view.SurfaceHolder android.view.SurfaceView.getHolder()'