RecyclerView:没有连接适配器;使用retro fit来获取json时跳过布局

时间:2017-02-15 14:02:58

标签: android android-recyclerview retrofit

我正在使用retrofit从REST api获取json数据。我实施了改造,但在启动应用程序后,它崩溃并出现错误:RecyclerView: No adapter attached; skipping layout。我正在使用之前的答案,但没有一个能解决我的问题。这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cinsio_notif);

    initViews();


private void initViews() {
    recyclerView = (RecyclerView) findViewById(R.id.noti_recyle);
    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(layoutManager);
    loadJSON();
}


private void loadJSON() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://chikoop.com/api/index.php/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    notiIterface request = retrofit.create(notiIterface.class);
    Call<JSONResponse> call = request.getNotification(userId, sessionId);
    call.enqueue(new Callback<JSONResponse>() {
        @Override
        public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
            JSONResponse jsonResponse = response.body();
            data = new ArrayList<>(Arrays.asList(jsonResponse.getAndroid()));
            adapter = new noti_adapter(data, getApplicationContext());
            recyclerView.setAdapter(adapter);
        }

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

        }
    });
}

我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/textColor">

    <include
        android:id="@+id/custome_actionbar"
        layout="@layout/cusmote_actionbar"
        android:layout_width="match_parent"
        android:layout_height="60dp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/noti_recyle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/custome_actionbar">

    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

---------------------------------&GT;

如果有人需要我的适配器代码:

    List<noti_sec> noti_list;
    private Context context;

    //Constructor of this class
    public noti_adapter(List<noti_sec> noti_list, Context context){
        super();
        //Getting all superheroes
        this.noti_list = noti_list;
        this.context = context;
    }


    public static class notiViewHolder extends RecyclerView.ViewHolder {

        CircleImageView profile_pic;
        TextView name;
        TextView about_noti;
        TextView noti_time;
        ImageView from;


        public notiViewHolder(View v) {
            super(v);
            profile_pic = (CircleImageView) v.findViewById(R.id.profile_image);
            name = (TextView) v.findViewById(R.id.name);
            about_noti = (TextView) v.findViewById(R.id.notif_about);
            noti_time = (TextView) v.findViewById(R.id.noti_time);
            from = (ImageView) v.findViewById(R.id.msg_img);
        }
    }

    @Override
    public noti_adapter.notiViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.noti_msg_screen, parent, false);
        return new notiViewHolder(view);
    }

    @Override
    public void onBindViewHolder(notiViewHolder holder, final int position) {
        holder.profile_pic.setImageResource(R.drawable.border_frame1);
        holder.name.setText(noti_list.get(position).getUsername());
        holder.about_noti.setText(noti_list.get(position).getNoti_msg());

        Picasso.with(context)
                .load(image)
                .into(holder.from);
    }


    @Override
    public int getItemCount() {
        return noti_list.size();
    }

错误日志cat:

02-15 20:12:16.247 13338-13338/com.release.chikoopapp E/RecyclerView: No adapter attached; skipping layout
02-15 20:12:16.248 13338-13338/com.release.chikoopapp E/RecyclerView: No adapter attached; skipping layout
02-15 20:12:16.406 13338-13338/com.release.chikoopapp D/AndroidRuntime: Shutting down VM
02-15 20:12:16.415 13338-13338/com.release.chikoopapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.release.chikoopapp, PID: 13338
                                                                        java.lang.RuntimeException: Empty builders!
                                                                            at com.nightonke.boommenu.ExceptionManager.judge(ExceptionManager.java:71)
                                                                            at com.nightonke.boommenu.ExceptionManager.judge(ExceptionManager.java:27)
                                                                            at com.nightonke.boommenu.BoomMenuButton.doLayoutJobs(BoomMenuButton.java:346)
                                                                            at com.nightonke.boommenu.BoomMenuButton.access$600(BoomMenuButton.java:55)
                                                                            at com.nightonke.boommenu.BoomMenuButton$8.run(BoomMenuButton.java:1013)
                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:135)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:372)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

1 个答案:

答案 0 :(得分:0)

创建RecyclerView后不立即设置适配器可能会导致此问题。由于您需要下载要显示的数据,您可能需要设置一个带有空数据集的适配器,并在以后将数据设置为该数据集。

可以使用类似下面的辅助方法来避免在向适配器通知数据集中的更改时出现其他错误。

public void resetRecyclerView(final List<noti_sec> data) {
final Handler handler = new Handler();
handler.post(new Runnable() {
    @Override
    public void run() {
        if (recyclerView.isComputingLayout()) {
            handler.post(this);
        } else {
            noti_adapter adapter = (noti_adapter)recyclerView.getAdapter();
            adapter.setData(data);// custom method in your adapter
            adapter.notifyDataSetChanged();
        }
    }
});
}