RecyclerView:没有连接适配器;跳过布局

时间:2015-08-02 09:43:11

标签: java android android-recyclerview recycler-adapter

我收到了错误"没有适配器;跳过布局"在logcat中。但是在我的手机上显示它似乎没有问题。我想知道导致错误的原因。

先谢谢。

OnCreate方法:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_appbar);
    context=this;

    toolbar=(Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayShowHomeEnabled(true);

    NavFragment drawerFragment = (NavFragment)
            getSupportFragmentManager().findFragmentById(R.id.nav_frag);
    drawerFragment.setUp(R.id.nav_frag, (DrawerLayout) findViewById(R.id.drawerLayout), toolbar);
    listEvents= new ArrayList<>();



    Parse.enableLocalDatastore(this);
    Parse.initialize(this, "xxxxxxxxxx", "xxxxxxxxxxx");

    //TEST PARSE

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Test");
    //query.whereEqualTo("playerName", "Dan Stemkoski");
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> eventList, ParseException e) {
            if (e == null) {
                Log.d("Events", "Retrieved " + eventList.size() + " Events");

                for (int i = 0; i < eventList.size(); i++) {

                    Events events = new Events();
                    events.setTitle((String) eventList.get(i).get("teststr"));
                    events.setId(String.valueOf(eventList.get(i).get("teststr")));
                    listEvents.add(events);
                }
                eventAdapter= new EventAdapter(context);
                eventAdapter.setEventList(listEvents);
                eventsList.setAdapter(eventAdapter);
            } else {
                Log.d("score", "Error: " + e.getMessage());
            }
        }
    });


    eventsList=(RecyclerView)findViewById(R.id.list_events);
    eventsList.setLayoutManager(new LinearLayoutManager(this));



}

EventAdapter:

public class EventAdapter extends RecyclerView.Adapter<EventAdapter.ViewHolderEvents> {
    private LayoutInflater layoutInflater;
    private ArrayList<Events> listEvents= new ArrayList<>();
    public EventAdapter(Context context){
        layoutInflater=LayoutInflater.from(context);
    }

    public void setEventList(ArrayList<Events> listEvents){
        this.listEvents=listEvents;
        notifyItemRangeChanged(0,listEvents.size());
    }

    @Override
    public ViewHolderEvents onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view=layoutInflater.inflate(R.layout.custom_event_row,viewGroup,false);
        ViewHolderEvents viewHolder =new ViewHolderEvents(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolderEvents viewHolderEvents, int i) {
        Events currentEvent= listEvents.get(i);
        viewHolderEvents.title.setText(currentEvent.getTitle());
        viewHolderEvents.id.setText(currentEvent.getId());

    }

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

    static class ViewHolderEvents extends RecyclerView.ViewHolder{
        private TextView title;
        private TextView id;

        public ViewHolderEvents(View itemView) {
            super(itemView);
            title=(TextView)itemView.findViewById(R.id.title);
            id=(TextView)itemView.findViewById(R.id.description);
        }
    }

}

activity_main_appbar:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include
    android:id="@+id/app_bar"
    layout="@layout/app_bar" />
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"

    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/list_events"
            android:layout_height="match_parent"
            android:layout_width="match_parent"/>

    </FrameLayout>
    <fragment
        android:id="@+id/nav_frag"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_nav"
        android:name="com.squaredbytes.eventlane.NavFragment"
        tools:layout="@layout/fragment_nav" />

</android.support.v4.widget.DrawerLayout>

logcat的:

08-02 14:52:51.344  15877-15932/com.squaredbytes.eventlane I/OpenGLRenderer﹕ Initialized EGL, version 1.4
08-02 14:52:51.456  15877-15932/com.squaredbytes.eventlane D/OpenGLRenderer﹕ Enabling debug mode 0
08-02 14:52:51.486  15877-15877/com.squaredbytes.eventlane E/RecyclerView﹕ No adapter attached; skipping layout
08-02 14:52:51.615  15877-15877/com.squaredbytes.eventlane E/RecyclerView﹕ No adapter attached; skipping layout
08-02 14:52:53.521  15877-15877/com.squaredbytes.eventlane D/Events﹕ Retrieved 4 Events
08-02 14:57:41.831  15877-15888/com.squaredbytes.eventlane W/art﹕ Suspending all threads took: 8.652ms

2 个答案:

答案 0 :(得分:1)

这是因为您稍后将适配器附加到RecyclerView。您可以先设置空适配器,然后在获取数据时将数据填充到适配器。

您可以查看SH.的回答here以供参考。

答案 1 :(得分:0)

我遇到了同样的问题并尝试了关于它的所有答案。然后我去吃午饭然后打开我的电脑。那个错误不再出现了。也许重新启动PC可能会解决它?