提问人:Davencode 提问时间:8/29/2021 更新时间:8/29/2021 访问量:270
为什么我的应用在放置回收器视图时崩溃?
Why my app crashes when I put the recycler view?
问:
我遵循了 youtube 上的指南,它帮助我在 Java 中做一个 RecyclerView,但我是初学者。所以编译器说没关系,但它崩溃了,我不知道为什么。这是我关于回收器视图的代码页面。
在主页.java中:
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
然后在 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;
}
}
答:
0赞
Vatsal Dholakiya
8/29/2021
#1
您正在使用 并且还在 XML 文件中,这并不重要。设置 setHasFixedSize(true) 时,无法更改 RecyclerView 的高度和宽度。mRecyclerView.setHasFixedSize(true);
android:layout_height="wrap_content"
因此,请删除或设置recyclerView的高度作为match_parent。mRecyclerView.setHasFixedSize(true);
评论
0赞
Davencode
8/31/2021
感谢您通知我我犯的这个错误。
评论
No debuggable process
package name