为什么我的应用程序崩溃时,我把回收器视图?



我跟随youtube上的一个指南,它帮助我在Java中做一个RecyclerView,但我是一个初学者。编译器说没问题,但是它崩溃了,我不知道为什么。这是我的代码关于回收器视图的页面。

public class HomePage extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
NavigationView navigationView;
Toolbar toolbar;
private AppCompatButton getstart, augurio;
//istanzio recyclerview
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
//fine istanziata
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
//Per il buongiorno con silente
ArrayList<ExampleItem> exampleList = new ArrayList<>();
exampleList.add(new ExampleItem(R.drawable.ic__365678_fb_facebook_facebook_logo_icon, "Line1", "Line2"));
exampleList.add(new ExampleItem(R.drawable.ic__365678_fb_facebook_facebook_logo_icon, "Line1", "Line2"));
exampleList.add(new ExampleItem(R.drawable.ic__365678_fb_facebook_facebook_logo_icon, "Line1", "Line2"));
mRecyclerView = findViewById(R.id.RecyclerViewBuongiorno);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ExampleAdapter(exampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
//fine buongiorno silente

then in ExampleAdapter.java:

public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private ArrayList<ExampleItem> mExampleList;
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1, mTextView2;
public ExampleViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.Imageview);
mTextView1 = itemView.findViewById(R.id.textview);
mTextView2 = itemView.findViewById(R.id.textview2);
}
}
public ExampleAdapter(ArrayList<ExampleItem> exampleList){
mExampleList = exampleList;
}
@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getmImageResource());
holder.mTextView1.setText(currentItem.getmText1());
holder.mTextView2.setText(currentItem.getmText2());

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

}

然后最后一页java ExampleItem.java:

public class ExampleItem {
private int mImageResource;
private String mText1;
private String mText2;
public ExampleItem(int imageResource, String text1, String text2){
mImageResource = imageResource;
mText1 = text1;
mText2 = text2;
}
public int getmImageResource(){
return mImageResource;
}
public String getmText1(){
return mText1;
}
public String getmText2(){
return mText2;
}

}

您在XML文件中使用mRecyclerView.setHasFixedSize(true);android:layout_height="wrap_content",这不是相当大的。当你设置setHasFixedSize(true)时,你不能改变回收视图的高度和宽度。

因此,请删除mRecyclerView.setHasFixedSize(true);或将recyclerView的高度设置为match_parent.

相关内容

  • 没有找到相关文章

最新更新