【问题标题】:No adapter attached; skipping layout skipped 1 till 2 frames未连接适配器;跳过布局跳过 1 到 2 帧
【发布时间】:2019-11-01 22:33:13
【问题描述】:

我已经尝试寻找答案,但没有一个有效,但我相信这段代码有问题,调试器说

这是我文件的链接:TodoListApp

跳过了 1 帧!应用程序可能在其主线程上做了太多工作

// working with data
    ourdoes = findViewById(R.id.ourdoes);
    ourdoes.setLayoutManager(new LinearLayoutManager(this));
    list = new ArrayList<MyDoes>();

    // get data from firebase
    reference = FirebaseDatabase.getInstance().getReference().child("SeaLab13");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // set code to retrive data and replace layout
            for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
            {
                MyDoes p = dataSnapshot1.getValue(MyDoes.class);
                list.add(p);
            }
            doesAdapter = new DoesAdapter(MainActivity.this, list);
            ourdoes.setAdapter(doesAdapter);
            doesAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // set code to show an error
            Toast.makeText(getApplicationContext(), "No Data", Toast.LENGTH_SHORT).show();
        }
    });

【问题讨论】:

  • @SammyT 你好,你能修改我的代码吗,因为我在你提供的链接中看到了你的代码,但我不明白我需要在哪一部分更改它。谢谢
  • 您是否阅读了有关导致此错误的原因的说明?您需要澄清哪一部分?
  • @SammyT 是的,我做到了,关于错误的代码和解释是相似的,我认为那是因为我和他一样看了同样的教程
  • 您介意查看和更改我的代码吗?我给你链接

标签: java android


【解决方案1】:

在你的 onCreate 中:

// Set up your RecyclerView with the appropriate Layout Manager
RecyclerView myRecycler = findViewById(R.id.my_recycler_id);
myRecycler.setLayoutManager(new LinearLayoutManager(this));

// Create your data set
myData = new ArrayList<MyDataType>();

// Create an instance of your adapter passing the data set into the constructor
myAdapter = new MyAdapter(this, myData);

// Set the Adapter on the RecyclerView directly within onCreate
// so that it doesn't get skipped
myRecycler.setAdapter(myAdapter);

在您的事件侦听器回调中:

@Override
public void onDataChange(DataSnapshot snapshot){
    // Add the new data to your data set ex. myData.add(newData)
    // ...

    // After adding to the data set,
    // update the data using a custom function you define in your Adapter's class
    myAdapter.updateData(myData);
}

在您的 Adapter 类中,创建一个函数来更新您的 Adapter 的数据集:

public void updateData(ArrayList<MyDataType> newDataSet){
    myAdapterDataSet = newDataSet;

    // Let the Adapter know the data has changed and the view should be refreshed
    notifyDataSetChanged();
}

【讨论】:

  • 非常感谢@SammyT 现在可以正常工作,但我还有另一个问题是调试器说:I/Choreographer:跳过 8 帧!应用程序可能在其主线程上做了太多工作。 I/Choreographer:跳过了 76 帧!应用程序可能在其主线程上做了太多工作。 I/Choreographer:跳过了 172 帧!应用程序可能在其主线程上做了太多工作。 I/Choreographer:跳过了 350 帧!应用程序可能在其主线程上做了太多工作。一旦跳过超过 200 帧,应用就会冻结
  • @IanIndratama 你会想要研究 Android 的线程和后台处理。
猜你喜欢
  • 2021-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-15
  • 2021-04-24
  • 2020-03-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多