Java RecyclerView:未连接适配器;跳过活动中的布局
我已经看过了其他的问题,但没有一个答案对我有用 尝试为RecyclerView充气时出现的错误是: E/RecyclerView:未连接适配器;跳过布局 这是我试图将ArrayList的一个示例项充气到我的RecyclerView中:Java RecyclerView:未连接适配器;跳过活动中的布局,java,android,android-recyclerview,Java,Android,Android Recyclerview,我已经看过了其他的问题,但没有一个答案对我有用 尝试为RecyclerView充气时出现的错误是: E/RecyclerView:未连接适配器;跳过布局 这是我试图将ArrayList的一个示例项充气到我的RecyclerView中: //Chord Obj: Chord(int images[], String name, Int sound) chords.add(new Chord(new int[]{ R.drawable.do_maggiore, R.dra
//Chord Obj: Chord(int images[], String name, Int sound)
chords.add(new Chord(new int[]{
R.drawable.do_maggiore, R.drawable.do_maggiore_alt1, R.drawable.do_maggiore_alt2,
R.drawable.do_maggiore_alt3, R.drawable.do_maggiore_alt4, R.drawable.do_maggiore_alt5,
R.drawable.do_maggiore_alt6}, Resources.getSystem().getString(R.string.c), R.raw.do_maggiore));
这是我的列表\u Adapter.java
:
public class List_adapter extends RecyclerView.Adapter<List_adapter.MyViewHolder> {
private ArrayList<Chord> list;
Context c;
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView name;
private ImageView image;
private MyViewHolder(View v) {
super(v);
name = (TextView) v.findViewById(R.id.name);
image = (ImageView) v.findViewById(R.id.chord_image);
}
}
public List_adapter(Context c, ArrayList<Chord> list) {
this.list = list;
this.c = c;
}
@Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override public void onBindViewHolder(MyViewHolder holder, int pos) {
Chord chord = list.get(pos);
holder.name.setText(chord.getName());
holder.image.setImageResource(chord.getImages()[0]);
}
@Override public int getItemCount() {return list.size();}
}
最后,这是我的xml:
...
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chords_recycler"
android:layout_alignParentStart="true"
android:layout_marginTop="25dp" />
...
。。。
尝试将您的RecyclerView高度设置为“匹配父项”,然后查看输出是否有效。尝试将您的RecyclerView高度设置为“匹配父项”,然后查看输出是否有效。设置适配器时使用以下命令:-
List_adapter adapter = new List_adapter(this, chords);
RecyclerView.LayoutManager mLayoutManager1 = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager1);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
而不是
final List_adapter adapter = new List_adapter(this, chords);
recyclerView.setAdapter(adapter);
设置适配器时请使用以下命令:-
List_adapter adapter = new List_adapter(this, chords);
RecyclerView.LayoutManager mLayoutManager1 = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager1);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
而不是
final List_adapter adapter = new List_adapter(this, chords);
recyclerView.setAdapter(adapter);
它不会显示,因为您正在尝试在onCreate方法中对RecyclerView进行充气。请将您的RecyclerView代码移动到活动的“恢复”方法中
/* gets the list */
chords = new ArrayList<>();
ChordsList list = new ChordsList();
list.createList();
chords = list.getList();
/* puts the list into the recyclerView */
recyclerView = (RecyclerView) findViewById(R.id.chords_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(Main_list_activity.this,
LinearLayoutManager.VERTICAL, false));
recyclerView.setHasFixedSize(true);
final List_adapter adapter = new List_adapter(this, chords);
recyclerView.setAdapter(adapter);
/*获取列表*/
和弦=新的ArrayList();
和弦列表=新的和弦列表();
createList();
和弦=list.getList();
/*将列表放入recyclerView*/
recyclerView=(recyclerView)findViewById(R.id.chords\u recycler);
recyclerView.setLayoutManager(新的LinearLayoutManager(主列表)活动,
LinearLayoutManager.VERTICAL,false);
recyclerView.setHasFixedSize(true);
最终列表\适配器=新列表\适配器(此为和弦);
recyclerView.setAdapter(适配器);
它不显示,因为您试图在onCreate方法中对RecyclerView进行充气。请将您的RecyclerView代码移动到活动的“恢复”方法中
/* gets the list */
chords = new ArrayList<>();
ChordsList list = new ChordsList();
list.createList();
chords = list.getList();
/* puts the list into the recyclerView */
recyclerView = (RecyclerView) findViewById(R.id.chords_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(Main_list_activity.this,
LinearLayoutManager.VERTICAL, false));
recyclerView.setHasFixedSize(true);
final List_adapter adapter = new List_adapter(this, chords);
recyclerView.setAdapter(adapter);
/*获取列表*/
和弦=新的ArrayList();
和弦列表=新的和弦列表();
createList();
和弦=list.getList();
/*将列表放入recyclerView*/
recyclerView=(recyclerView)findViewById(R.id.chords\u recycler);
recyclerView.setLayoutManager(新的LinearLayoutManager(主列表)活动,
LinearLayoutManager.VERTICAL,false);
recyclerView.setHasFixedSize(true);
最终列表\适配器=新列表\适配器(此为和弦);
recyclerView.setAdapter(适配器);
在活动中插入以下内容:
public class TestActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<Chord> chords;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
setAdapter();
}
private void setAdapter() {
chords = new ArrayList<>();
chords.add(new Chord(new int[]{
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher},"Name", R.raw.audio_in_raw));
List_adapter adapter = new List_adapter(chords, this);
RecyclerView.LayoutManager mLayoutManager1 = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager1);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
}
而xml是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="70dp"
android:weightSum="10"
>
<ImageView
android:layout_weight="2"
android:id="@+id/ivImage"
android:layout_width="0dp"
android:layout_height="match_parent"></ImageView>
<TextView
android:gravity="center"
android:id="@+id/tvText"
android:layout_weight="6"
android:layout_width="0dp"
android:layout_height="match_parent" />
在活动中插入以下内容:
public class TestActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<Chord> chords;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
setAdapter();
}
private void setAdapter() {
chords = new ArrayList<>();
chords.add(new Chord(new int[]{
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher},"Name", R.raw.audio_in_raw));
List_adapter adapter = new List_adapter(chords, this);
RecyclerView.LayoutManager mLayoutManager1 = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager1);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
}
而xml是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="70dp"
android:weightSum="10"
>
<ImageView
android:layout_weight="2"
android:id="@+id/ivImage"
android:layout_width="0dp"
android:layout_height="match_parent"></ImageView>
<TextView
android:gravity="center"
android:id="@+id/tvText"
android:layout_weight="6"
android:layout_width="0dp"
android:layout_height="match_parent" />
我已经设法把它修好了。这个问题其实很明显,但我在写问题时并没有完全理解
我只是创建了另一个活动,并将布局设置为内容抽屉。这是错误的,因为所有代码都需要放入抽屉.class
(如果您使用的是Android Studio中的导航抽屉预设)
这就是调试器没有显示任何内容的原因,因为它从未到达该类。而且因为代码从未运行过,所以它当然不能显示任何内容
谢谢你给我的答案 我已经设法修好了。这个问题其实很明显,但我在写问题时并没有完全理解
我只是创建了另一个活动,并将布局设置为内容抽屉。这是错误的,因为所有代码都需要放入抽屉.class
(如果您使用的是Android Studio中的导航抽屉预设)
这就是调试器没有显示任何内容的原因,因为它从未到达该类。而且因为代码从未运行过,所以它当然不能显示任何内容
谢谢你给我的答案 谢谢你的回答。我已经试过了,但是没有luckcan你可以检查一下recyclerview是否与scrollview兼容。谢谢你的回答。我已经尝试过了,但是没有luckcan您可以检查recyclerview是否与scrollview一起使用。谢谢您的回答,但是这一个不起作用。您可以确保在数组列表中添加正确的数据,因为您希望在适配器中设置。您是什么意思?您可以看到上面的代码。对我来说,和弦对象有一组图像、一个名称字符串和一个音频文件,看起来不错。这就是我在和弦模型中定义的一切我已经解决了你的问题。。我正在添加带有新答案的代码谢谢你的回答,但是这个答案不起作用。你能确保你在数组列表中添加了正确的数据,就像你想在适配器中设置的那样。你是什么意思?您可以看到上面的代码。对我来说,和弦对象有一组图像、一个名称字符串和一个音频文件,看起来不错。这就是我在和弦模型中定义的一切我已经解决了你的问题。。我正在添加带有新答案的代码谢谢,我试过了。即使我在onResume()中有代码,列表也不会显示谢谢,我试过了。即使我有onResume()中的代码,列表也不会显示。如果您仍然面临问题,请告诉我。再次感谢您的帮助。基本上是xml改变了。实际上,我有一个row.xml,它看起来像您编写的,适配器将图像和字符串放在那里,然后将该行放入回收器中。请看一看编辑后的问题您知道调试器为什么是空的吗?适配器和模型类中也有一些小变化。。。。。你可以复制并粘贴你的代码。然后你会得到不同的结果。如果你仍然面临问题,请告诉我。再次感谢你的帮助。基本上是xml改变了。实际上,我有一个row.xml,它看起来像您编写的,适配器将图像和字符串放在那里,然后将该行放入回收器中。请看一看编辑后的问题您知道调试器为什么是空的吗?适配器和模型类中也有一些小变化。。。。。你可以复制并粘贴你的代码,然后你就会得到不同的结果