没有适配器;跳过布局onCreateView()

时间:2016-02-21 10:10:10

标签: java android adapter android-recyclerview

尝试在我的应用程序中实现RecyclerView,我不明白为什么我会“没有连接适配器;跳过布局”我认为我在onCreateView中清楚地设置了它。一些其他帖子建议将其移动到Oncreate(),但是当我这样做时,我会得到更多错误。如果有帮助的话我跟着这个tutorial。这是我的主要活动类

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

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

<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"/>

(是的,该应用程序由memes组成,以前的版本虽然已经吸引了很多用户)

2 个答案:

答案 0 :(得分:3)

尝试将活动的onCreateView()方法中的代码移至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的答案返回。

答案 1 :(得分:1)

你能试试吗?我看了我正在使用的回收站视图代码,发现了这个区别:

@Override

public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 

{View view = inflater.inflate(R.layout.meme_item, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return view;}

在此处返回视图,而不是返回null