gpt4 book ai didi

android - RecyclerView 致命异常 :main

转载 作者:搜寻专家 更新时间:2023-11-01 08:33:45 25 4
gpt4 key购买 nike

有人可以帮助我知道是什么导致了我的 recyclerview 中的这个错误我已经尝试更改变量名称

Adapter1.java:48

但它仍然给我同样的错误。

java.lang.NullPointerException
at ke.co.clickaway.squirrel.clickawaytechnologies.adpter.Adapter1.getItemCount(Adapter1.java:48)

代码如下

public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
ArrayList<DataList> list;
//in the onCreate Bundle
RecyclerView rview=(RecyclerView) findViewById(R.id.rview);
Adapter1 adapter1=new Adapter1(this,list);
rview.setAdapter(adapter1);
StaggeredGridLayoutManager lm=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
rview.setLayoutManager(lm);

信息类

public class DataList {
public int imageView;
public String title;

public static List<DataList> getData(){
ArrayList<DataList> mList=new ArrayList<>();
int[] imag={R.drawable.web_ic,R.drawable.palette_ic,R.drawable.pencil,R.drawable.sware_ic,R.drawable.phone_ic};
String[] lable={"Web Development","Branding","Graphic Design","Systems","Mobile Apps"};
for(int i=0;i<lable.length&& i<imag.length;i++){
DataList dataList=new DataList();
dataList.title=lable[i];
dataList.imageView=imag[i];
mList.add(dataList);
}
return mList;
}
}

还有我的适配器

public class Adapter1 extends RecyclerView.Adapter<Adapter1.ViewHolder> {
private List<DataList> mList;
private Context mContext;
public Adapter1(Context context,List<DataList> list){
mList=list;
mContext=context;
}
private Context getContext(){
return mContext;
}
@Override
public Adapter1.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context=parent.getContext();
LayoutInflater inflater=LayoutInflater.from(context);
View customView=inflater.inflate(R.layout.custom_view,parent,false);
ViewHolder viewHolder=new ViewHolder(customView);
return viewHolder;

}

@Override
public void onBindViewHolder(Adapter1.ViewHolder holder, int position) {
DataList dataList=mList.get(position);
holder.imag.setImageResource(dataList.imageView);
holder.lable.setText(dataList.title);
}

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

public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imag;
public TextView lable;

public ViewHolder(View itemView) {
super(itemView);
imag= (ImageView)itemView.findViewById(R.id.imageView1);
lable=(TextView)itemView.findViewById(R.id.textView1);
}
}
}

我的自定义 View

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
android:id="@+id/imageView1"
android:layout_gravity="center"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:src="@drawable/one" />
<TextView
android:id="@+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="dummy Text" />
</LinearLayout>

activty_main.xml 中的 RecyclerView fragment

<android.support.v7.widget.RecyclerView
android:layout_below="@id/appbarlayout"
android:id="@+id/rview"
android:layout_width="match_parent"
android:layout_height="match_parent" />

最佳答案

列表 为空。因此,在创建适配器或将适配器中的 getItemCount() 方法更改为此之前初始化列表:

@Override
public int getItemCount() {
return mList == null ? 0 : mList.size();
}

关于android - RecyclerView 致命异常 :main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38366825/

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