[英]Android RecycleView No Adapter Attached, Skipping Layout
I have search an error about no adapter attached. 我搜索到有关未连接适配器的错误。 There is so many duplicated question about it and have different answer, and nothing fix my error.
有太多重复的问题,答案也不同,没有任何东西可以解决我的错误。
So, I working on CRUD Apps with Retrofit. 因此,我使用Retrofit开发CRUD应用程序。 I can insert data to database through application but when i tried to view data my RecyclerView didn't appear.
我可以通过应用程序将数据插入数据库,但是当我尝试查看数据时,我的RecyclerView没有出现。
Logcat Message : Logcat消息:
RecyclerView: No adapter attached; skipping layout
And this my MainActivity.class 这是我的MainActivity.class
public class MainActivity extends AppCompatActivity {
Button btIns;
ApiInterface mApiInterface;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
public static MainActivity ma;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btIns = (Button) findViewById(R.id.btIns);
btIns.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, InsertActivity.class));
}
});
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mApiInterface = ApiClient.getClient().create(ApiInterface.class);
ma=this;
refresh();
}
public void refresh() {
Call<GetKontak> kontakCall = mApiInterface.getKontak();
kontakCall.enqueue(new Callback<GetKontak>() {
@Override
public void onResponse(Call<GetKontak> call, Response<GetKontak>
response) {
List<Kontak> KontakList = response.body().getListDataKontak();
Log.d("Retrofit Get", "Jumlah data Kontak: " +
String.valueOf(KontakList.size()));
mAdapter = new KontakAdapter(KontakList);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onFailure(Call<GetKontak> call, Throwable t) {
Log.e("Retrofit Get", t.toString());
}
});
}
}
Retrofit callbacks are executed in a background thread, where you can't call setAdapter
. 改造回调在后台线程中执行,您无法在其中调用
setAdapter
。 Create the adapter and set it in the main thread, by calling mRecyclerView.post(runnable)
, with runnable containing the code you need to execute on the main thread. 通过调用
mRecyclerView.post(runnable)
创建适配器并将其设置在主线程中,其中runnable包含您需要在主线程上执行的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.