未连接适配器;跳过布局onCreateView() [英] No adapter attached; skipping layout onCreateView()

查看:111
本文介绍了未连接适配器;跳过布局onCreateView()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在我的应用程序中实现RecyclerView时,我不明白为什么会收到未连接适配器;跳过布局的原因,我想我已经在onCreateView中明确设置了它。其他一些帖子建议将其移至Oncreate(),但是当我这样做时,我会收到更多错误。如果有帮助,我按照了教程的要求。这是我的主要活动课

Trying to implement RecyclerView into my application, I don't understand why i'm getting "No adapter attached; skipping layout" I thought i clearly set it up in the onCreateView. Some other posts suggested to move it to Oncreate() but when i do that i get many more errors. I followed this tutorial if it helps. Here is my Main Activity class

我不认为这是另一个问题的重复,尽管可能是相似的。当移到onCreate()时,整个应用程序崩溃。

I do not believe this is a duplicate of another question, although it might be similar. when moved to onCreate() the entire application crashes.

public class MainActivity extends ActionBarActivity  {

private Toolbar toolbar;
private RecyclerView recyclerView;
private Adapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState){
    View layout=inflater.inflate(R.layout.activity_main, container, false);
    recyclerView= (RecyclerView) layout.findViewById(R.id.meme_list);
    adapter = new Adapter(this,getData());
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);

    return layout;

}

public static List<Memes> getData(){
    List<Memes> data = new ArrayList<>();
    String[] names ={"meme1", "meme2", "meme3"};
    for(int i=0; i<names.length; i++)
    {
        Memes meme = new Memes();
        meme.name=names[i];
        data.add(meme);

    }
    return data;
}

这里是适配器类

public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {

private LayoutInflater inflater;
List<Memes> data = Collections.emptyList();
public Adapter(Context context, List<Memes> data){
    inflater = LayoutInflater.from(context);
    this.data=data;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.meme_item, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    return null;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Memes current=data.get(position);
    holder.title.setText(current.name);


}

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

class MyViewHolder extends RecyclerView.ViewHolder{
    TextView title;

    public MyViewHolder(View itemView) {
        super(itemView);
        title=(TextView)itemView.findViewById(R.id.name);

    }
}

主要活动xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include
    android:id="@+id/tool_bar"
    layout="@layout/tool_bar">

</include>

<android.support.v7.widget.RecyclerView
    android:layout_marginTop="@dimen/activity_horizontal_margin"
    android:id="@+id/meme_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

用于回收站视图的XML项目

XML for layout of recycler view items

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

<TextView
    android:id="@+id/name"
    android:layout_gravity="left"
    android:padding="8dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Dummy text"/>

(是,该应用包含模因,但以前的版本吸引了很多用户)

(Yes, the app consists of memes, previous version pulled in a lot of users though)

推荐答案

尝试在 onCreateView中移动代码()您的Activity的方法,以 onCreate()

Try move the code in onCreateView() method of your Activity to onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    recyclerView = (RecyclerView) findViewById(R.id.meme_list);
    adapter = new Adapter(this,getData());
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);

}

您还需要返回 ViewHolder 作为@YasinKaçmaz的答案。

You also need to return ViewHolder as @Yasin Kaçmaz 's answer.

这篇关于未连接适配器;跳过布局onCreateView()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆