0

Recently i am working on RecycleView nad Retrofit for Json practise. I have managed everything but when i run my app it crushed and shows that no adapter attached.

This is my MainActivity class

public class MainActivity extends AppCompatActivity{
public MyRecycleViewAdapter adapter;
public RecyclerView recyclerView;
List<Movie> movies;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    LinearLayoutManager llm= new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
    recyclerView.setLayoutManager(llm);

    ApiINterface apiINterface = ApiClient.getClient().create(ApiINterface.class);
    Call<Movies> call = apiINterface.getSongList("just_added");

    call.enqueue(new Callback<Movies>() {
        @Override
        public void onResponse(Call<Movies> call, Response<Movies> response) {

           movies = response.body().getMovies();

            adapter = new MyRecycleViewAdapter(MainActivity.this , movies);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFailure(Call<Movies> call, Throwable t) {

        }
    });

}}

And this is my Logcat:

 06-14 17:05:21.16125804-25804/com.example.lolipop.retrofitexamplewithhindisong E/RecyclerView:
    No adapter attached; skipping layout 06-14 17:05:21.331 25804-25804/com.example.lolipop.retrofitexamplewithhindisong E/RecyclerView: No adapter attached; skipping layout
    06-14 17:05:21.371 25804-25804/com.example.lolipop.retrofitexamplewithhindisong E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException                                                                                                 at com.example.lolipop.retrofitexamplewithhindisong.MainActivity$1.onResponse(MainActivity.java:45) at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:155)
    at android.app.ActivityThread.main(ActivityThread.java:5511)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)                                at dalvik.system.NativeStart.main(Native Method)

What's the problem i can't find.

M.Waqas Pervez
  • 2,492
  • 2
  • 19
  • 33
pavel
  • 1,603
  • 22
  • 19
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Harshad Pansuriya Jun 14 '16 at 11:15
  • why this question will duplicate this is about RecycleView Adapter problem and you tag that is null point problem @Ironman – pavel Jun 14 '16 at 11:17
  • 1
    did you check if `response.body().getMovies();` is not returning null value ? – M.Waqas Pervez Jun 14 '16 at 11:20
  • Also check if `onFailure` is being called instead of `onResponse`. – Ishita Sinha Jun 14 '16 at 11:22
  • Possible duplicate of [recyclerview No adapter attached; skipping layout](http://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout) – Janki Gadhiya Jun 14 '16 at 11:25
  • Tip: if you click MainActivity.java:45 on the logcat at the first line under null pointer exception it would show you the exact line which causes the error. Do that and show that exact line using a comment. The line should be inside the onResponse() method. – Malith Lakshan Jun 14 '16 at 11:28

1 Answers1

2

I think response.body().getMovies() returning null. Better to check for null -

if(response.body() != null && response.body().getMovies() != null && response.body().getMovies().size()> 0){
adapter = new MyRecycleViewAdapter(MainActivity.this , movies);
                recyclerView.setAdapter(adapter);
}
Neo
  • 3,546
  • 1
  • 24
  • 31