gpt4 book ai didi

java - 如何修复 "No adapter attached; skipping layout"

转载 作者:太空狗 更新时间:2023-10-29 14:35:24 25 4
gpt4 key购买 nike

我想在 RecycleView 中显示包裹在 fragment 中的 ArrayList,但是当我运行 apk 时,它没有显示 RV。当我在 logcat 上看到时,有注释“没有附加适配器;跳过布局”。

我尝试了很多方法来更改 fragment 和适配器,但终究没有奏效。由于本网站上的其他问题基于 API,我不明白,因为我是初学者。我试图找出一个基于Arraylist的问题,结果还是一样,没有一个解决方案是基于我的问题。

我将展示我的适配器、 fragment 和 mainActivity 中的代码,它会很长,但我想知道问题所在,因为我迷路了,一无所知。

fragment :

public class FilmFragment extends Fragment {


View view;
ArrayList<Film> list = new ArrayList<>();
RecyclerView rvCategory;
FilmDatas filmDatas;


public FilmFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_movie,container,false);
return view;}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState){
rvCategory = view.findViewById(R.id.rvCategory);
rvCategory.setHasFixedSize(true);
list.addAll(filmDatas.getListData());
showRecycleList();}
private void showRecycleList(){
rvCategory.setLayoutManager(new LinearLayoutManager(getActivity()));
FilmAdapter adapter = new FilmAdapter(getContext());
adapter.setListMovie(list);
rvCategory.setAdapter(adapter);}}

适配器:

public class FilmAdapter extends RecyclerView.Adapter<FilmAdapter.CategotyViewHolder> {
Context context;
private ArrayList<Film> listFilm;


public ArrayList<Film> getList(){
return listFilm; }

public void setListMovie(ArrayList<Film> list) {
this.listFilm = list;}

public FilmAdapter(Context context
) {this.context = context;}


@NonNull
@Override
public CategotyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row_film,parent,false);
return new CategotyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final CategotyViewHolder holder, int position) {
final Film film = listFilm.get(position);

Glide.with(holder.itemView.getContext()).load(film.getPhoto()).apply(new RequestOptions().override(100,100))
.into(holder.imgPhoto);
holder.tvName.setText(film.getName());
holder.tvFrom.setText(film.getFrom());
holder.tvDate.setText(film.getDate());


}

@Override
public int getItemCount() {

return getList().size();
}

public class CategotyViewHolder extends RecyclerView.ViewHolder {
ImageView imgPhoto;
TextView tvName,tvFrom,tvDate;

public CategotyViewHolder(@NonNull View itemView) {
super(itemView);
imgPhoto = itemView.findViewById(R.id.img_item_photo);
tvName = itemView.findViewById(R.id.tv_item_name);
tvFrom = itemView.findViewById(R.id.tv_item_from);
tvDate = itemView.findViewById(R.id.tv_date);}}

主要 Activity :

public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
FrameLayout frameLayout;
ViewPager viewPager;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout = findViewById(R.id.Frame);
tabLayout = findViewById(R.id.simpelTab);
viewPager = findViewById(R.id.viewPager);
viewPager.setAdapter(new sliderAdapter(getSupportFragmentManager()));

tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});

tabLayout.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});
}

private class sliderAdapter extends FragmentPagerAdapter {
String movie = "MOVIE";
String tvShow = "TV SHOW";
final String tabs[] = {movie, tvShow};

public sliderAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new FilmFragment();
case 1:
return new TvShowFragment();
}
return null;
}

@Override
public int getCount() {
return tabs.length; //count Tabs[] size: 2
}

@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
if (item.getItemId() == R.id.action_settings) {
Intent mIntent = new Intent(Settings.ACTION_LOCALE_SETTINGS);
startActivity(mIntent);
}
}

return super.onOptionsItemSelected(item);
}


}

电影数据:

public class FilmDatas { public static String [][] data = new String[][]{
{
"Venom (2018)","Investigative journalist Eddie Brock attempts a comeback following a scandal, but accidentally becomes the host of Venom, a violent, super powerful alien symbiote. Soon, he must rely on his newfound powers to protect the world from a shadowy organization looking for a symbiote of their own.","https://image.tmdb.org/t/p/w220_and_h330_face/2uNW4WbgBXL25BAbXGLnLqX71Sw.jpg","https://image.tmdb.org/t/p/w533_and_h300_bestv2/VuukZLgaCrho2Ar8Scl9HtV3yD.jpg","October 5, 2018"
}

};
public ArrayList<Film> getListData() {
ArrayList<Film> list = new ArrayList<>();
for (String[] aData : data) {
Film film = new Film();
film.setName(aData[0]);
film.setFrom(aData[1]);
film.setBackground(aData[2]);
film.setPhoto(aData[3]);
film.setDate(aData[4]);

list.add(film);
}
return list;
}
}

电影:(它包含可打包和设置/获取)

public class Film implements Parcelable {
private String name,from,background,photo,date;

public Film() {

}


public String getName() {return name;}
public void setName (String name) {this.name = name;}
public String getFrom() {return from;}
public void setFrom (String from) {this.from = from;}
public String getBackground() {return background;}
public void setBackground (String background) {this.background = background;}
public String getPhoto() {return photo;}
public void setPhoto (String photo) {this.photo = photo;}
public String getDate() {return date;}
public void setDate (String date) {this.date = date;}

protected Film(Parcel in) {
name = in.readString();
from = in.readString();
background = in.readString();
photo = in.readString();
date = in.readString();
}

public static final Creator<Film> CREATOR = new Creator<Film>() {
@Override
public Film createFromParcel(Parcel in) {
return new Film(in);
}

@Override
public Film[] newArray(int size) {
return new Film[size];
}
};

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(name);
parcel.writeString(from);
parcel.writeString(background);
parcel.writeString(photo);
parcel.writeString(date);
}
}

最佳答案

您的问题取决于您的 Fragment。

你已经声明了

FilmDatas filmDatas;

但是你从来没有实例化它,甚至没有在这里填充它的任何对象

 @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState){
rvCategory = view.findViewById(R.id.rvCategory);
rvCategory.setHasFixedSize(true);
list.addAll(filmDatas.getListData());
showRecycleList();
}

因此,没有数据可填充您的列表,您的 recyclerview 将不会显示任何内容

尝试像这样将元素添加到列表中进行测试

list.add(new FilmDatas("param1","param2"...))

此外,您将列表声明为 Film 类型,但您正在使用 FilmDatas 类型填充它

但我假设 filmData.getListData() 返回您要填充的 Film 类型的对象。

问题不在于“没有连接适配器;跳过布局” ,问题是您没有向列表中添加任何元素,并且当您将列表设置为适配器时,它不会绑定(bind)每个 recyclerview 行内的任何数据,并且 getItemCount() 返回 0,因此您的 recyclerview 不会显示任何内容。

关于java - 如何修复 "No adapter attached; skipping layout",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57147203/

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