I have a simple program that connect to an API and get some data from it.I use Retrofit
library.There are two activity. MainActivity
includes a RecyclerView
that show data received from the server. If press any item,application go to UserInfoActivity
and show detailed information of that item.My app when go UserInfoActivity
crashed after a few seconds and don't show information. However response
correctly received but the program gives an error
on txtId.setText(user.getId)
line. The error is E/RecyclerView: No adapter attached; skipping layout
In fact my problem starts when call Call<User> getUserInfo(@Path("id") int id);
in second Activity.
This is MainActivity
code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userRequest();
}
private void userRequest(){
APIInterface apiInterface= APIClient.getClient().create(APIInterface.class);
retrofit2.Call<ArrayList<User>> call= apiInterface.getUsers();
call.enqueue(new Callback<ArrayList<User>>() {
@Override
public void onResponse(retrofit2.Call<ArrayList<User>> call, Response<ArrayList<User>> response) {
if(response.isSuccessful()){
ArrayList<User> users = response.body();
setupRecycelerView(users);
}
}
@Override
public void onFailure(retrofit2.Call<ArrayList<User>> call, Throwable t) {
Log.i("RETROFIT","response not successful");
}
});
}
private void setupRecycelerView(ArrayList<User> users){
recyclerView=findViewById(R.id.RcyView);
adapter=new RecycelerAdapter(this,users);
linearLayoutManager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
}
This is onBindViewHolder
function of RecycelerAdapter
.
public void onBindViewHolder(@NonNull UserViewHolder holder, int position) {
final User user=values.get(position);
holder.txtName.setText(user.getName());
holder.txtPhone.setText(user.getPhone());
final Intent intent=new Intent(context, UserInfoActivity.class);
intent.putExtra("user",user);
holder.root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
context.startActivity(intent);
}
});
}
This is UserInfoActivity
code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_info);
txtId=findViewById(R.id.txtId);
txtName=findViewById(R.id.txtName);
txtPhone=findViewById(R.id.txtPhone);
Intent intent=getIntent();
User user=(User)intent.getSerializableExtra("user");
id=user.getId();
userInfoRequest();
}
private void userInfoRequest(){
APIInterface apiInterface= APIClient.getClient().create(APIInterface.class);
retrofit2.Call<User> call= apiInterface.getUserInfo(id);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if(response.isSuccessful()){
User user=response.body();
Log.e("MF","ON Response");
Log.e("MF",user.getName());
Log.e("MF",user.getPhone());
txtId.setText(user.getId());
txtName.setText(user.getName());
txtPhone.setText(user.getPhone());
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
Log.e("MF","on failure");
}
});
}