E / RecyclerView:未连接适配器;跳过布局错误17

时间:2018-11-10 15:47:13

标签: java android android-studio android-recyclerview adapter

继续出现以下错误:

E/RecyclerView: No adapter attached; skipping layout. 

如下所示,我已经设置了适配器,但是在OnDataChange()方法中,我认为这可能是问题所在,因为它需要在OnCreate()方法中,但是我似乎无法修复它。

@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_judge);

        RecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        RecyclerView.setHasFixedSize(true);
        RecyclerView.setLayoutManager(new LinearLayoutManager(this));

    ProgressCircle = findViewById(R.id.progress_circle);

    mUploads = new ArrayList<>();

    DatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");

    DatabaseRef.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                UploadClass upload = 
       postSnapshot.getValue(UploadClass.class);
                mUploads.add(upload);
            }
            Adapter = new ImageAdapter(JudgeActivity.this, mUploads);
            ProgressCircle.setVisibility(View.INVISIBLE);
            RecyclerView.setAdapter(Adapter);
         }

1 个答案:

答案 0 :(得分:0)

您已设置适配器,但为时已晚。该视图未加载任何适配器,因为您正在等待Firebase。

理想情况下,您应该首先设置适配器,因此请删除构造函数中的mUploads参数。

@Override
protected void onCreate(Bundle savedInstanceState) {
    recyclerView = (RecyclerView) find...
    recyclerView.setAdapter(new ImageAdapter(this));
    ...
}

最初,适配器将没有数据-但是RecyclerView将具有解决您问题的适配器。

您可以稍后设置数据,如果您存储适配器的实例,则可以:

adapter.setUploads(mUploads)

在您的Firebase通话中。