RecycleView没有连接适配器

时间:2016-06-14 11:12:51

标签: android json android-recyclerview

最近我正在为Json练习RecycleView nad Retrofit工作。 我已经管理了所有内容,但是当我运行我的应用程序时,它会崩溃并显示没有附加适配器。

这是我的 MainActivity类

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) {

        }
    });

}}

这是我的 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)

我无法找到的问题是什么。

1 个答案:

答案 0 :(得分:2)

我认为response.body().getMovies()返回null。 最好检查null -

if(response.body() != null && response.body().getMovies() != null && response.body().getMovies().size()> 0){
adapter = new MyRecycleViewAdapter(MainActivity.this , movies);
                recyclerView.setAdapter(adapter);
}