嗨,我在互联网上找到了一些代码,我正在尝试对其进行测试,但是我没有连接任何适配器,从而跳过了布局错误。我一直徒劳地寻找解决方案。
这是我的MainActivity类:
private ArrayList<Employee> employeeList;
private ProgressDialog pDialog;
private RecyclerView recyclerView;
private EmployeesAdapter eAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Data.. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
//Creating an object of our api interface
ApiService api = RetroClient.getApiService();
/**
* Calling JSON
*/
Call<EmployeeList> call = api.getMyJSON();
/**
* Enqueue Callback will be call when get response...
*/
call.enqueue(new Callback<EmployeeList>() {
@Override
public void onResponse(Call<EmployeeList> call, Response<EmployeeList> response) {
//Dismiss Dialog
pDialog.dismiss();
if (response.isSuccessful()) {
/**
* Got Successfully
*/
employeeList = response.body().getEmployee();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
eAdapter = new EmployeesAdapter(employeeList);
RecyclerView.LayoutManager eLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(eLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(eAdapter);
}
}
@Override
public void onFailure(Call<EmployeeList> call, Throwable t) {
pDialog.dismiss();
}
});
}
我收到此错误:E / RecyclerView:未连接适配器;跳过布局 有什么建议吗?
答案 0 :(得分:2)
错误是由于从“延迟”方法初始化recycerlview和适配器。因此要解决此问题,请执行以下操作:
首先按如下所示初始化阵列列表:
private ArrayList<Employee> employeeList = new ArrayList<>();
在api调用之前初始化您的recyclerview,这意味着在主线程中:
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
eAdapter = new EmployeesAdapter(employeeList);
RecyclerView.LayoutManager eLayoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(eLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(eAdapter);
然后在api响应中,仅当找到列表时才更新适配器:
if(response != null && response.body() != null && response.body().getEmployee() != null){
employeeList.clear();
employeeList.addAll(response.body().getEmployee());
if(eAdapter != null){
eAdapter.notifyDataSetChanged();
}
}
答案 1 :(得分:1)
在调用后台任务之前,始终先尝试初始化对象,必需的类,适配器(例如LinearLayoutManager)。也许这就是您遇到的问题。
我对您的代码进行了一些更改:
private ArrayList<Employee> employeeList;
private ProgressDialog pDialog;
private RecyclerView recyclerView;
private EmployeesAdapter eAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
employeeList = new ArrayList<>;
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
eAdapter = new EmployeesAdapter(employeeList);
RecyclerView.LayoutManager eLayoutManager = new
LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(eLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(eAdapter);
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Data.. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
//Creating an object of our api interface
ApiService api = RetroClient.getApiService();
/**
* Calling JSON
*/
Call<EmployeeList> call = api.getMyJSON();
/**
* Enqueue Callback will be call when get response...
*/
call.enqueue(new Callback<EmployeeList>() {
@Override
public void onResponse(Call<EmployeeList> call, Response<EmployeeList> response) {
//Dismiss Dialog
pDialog.dismiss();
if (response.isSuccessful()) {
/**
* Got Successfully
*/
if(!employeeList.isEmpty())employeeList.clear();
employeeList.addAll(response.body().getEmployee());
eAdapter.notifyDataSetChanged();
}
}
@Override
public void onFailure(Call<EmployeeList> call, Throwable t) {
pDialog.dismiss();
}
});
}
答案 2 :(得分:1)
错误消息已清除。
创建Activity时,您没有为recyclerView设置适配器。可能您认为您已经在onCreate方法中为recyclerView设置了适配器,但是有两个线程正在运行,一个是主线程,另一个线程是您为recylerView设置适配器的线程。这意味着,当需要附加recylerView适配器时,您为recylerView设置适配器的线程可能会阻止从网络获取数据。
您可以按照以下代码解决此问题!
nslookup adfs-sts.branch.tree.com