gpt4 book ai didi

java - 没有连接适配器;跳过布局 onCreateView()

转载 作者:行者123 更新时间:2023-11-29 03:00:12 26 4
gpt4 key购买 nike

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

我不认为这是另一个问题的重复,尽管它可能相似。当移动到 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);

}
}

主要 Activity 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>

用于回收器 View 项目布局的 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"/>

(是的,该应用程序由表情包组成,虽然之前的版本吸引了很多用户)

最佳答案

尝试将 Activity 的 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 作为@Yasin Kaçmaz 的回答。

关于java - 没有连接适配器;跳过布局 onCreateView(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35534789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com