【发布时间】:2019-03-24 20:08:30
【问题描述】:
当我将RecyclerView 设置为我的适配器时,我缺少一些东西。应用程序在运行时崩溃。谁能帮我确定问题?
// My adapter
public class Adapter extends RecyclerView.Adapter<Adapter.myViewHolder> {
private Context mContext;
private List<item> mData;
public Adapter(Context mContext, List<item> mData) {
this.mContext = mContext;
this.mData = mData;
}
@Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.card_item, parent, false);
return new myViewHolder(v);
}
@Override
public void onBindViewHolder(myViewHolder holder, int position) {
holder.background_img.setImageResource(mData.get(position).getBackground());
holder.profilePhoto.setImageResource(mData.get(position).getProfilePhoto());
holder.tv_title.setText(mData.get(position).getProfileName());
holder.tv_nbFollowers.setText(mData.get(position).getNbFollower() + " Followers");
}
@Override
public int getItemCount() {
return mData.size();
}
public static class myViewHolder extends RecyclerView.ViewHolder {
ImageView profilePhoto, background_img;
TextView tv_title, tv_nbFollowers;
public myViewHolder(View itemView) {
super(itemView);
profilePhoto = itemView.findViewById(R.id.profile_img);
background_img = itemView.findViewById(R.id.card_background);
tv_title = itemView.findViewById(R.id.card_title);
tv_nbFollowers = itemView.findViewById(R.id.card_nb_follower);
}
}
}
这就是我使用适配器设置RecyclerView 的方式
RecyclerView recList = findViewById(R.id.rv_list);
recList.setHasFixedSize(true);
List<item> mlist = new ArrayList<>();
mlist.add(new item(R.drawable.fish0, "Bass", R.drawable.profile0, 2500));
mlist.add(new item(R.drawable.fish1, "Mondo Bass", R.drawable.profile1, 3500));
mlist.add(new item(R.drawable.fish2, "Large Mouth Bass", R.drawable.profile2, 5500));
mlist.add(new item(R.drawable.fish3, "Bass", R.drawable.profile3, 10500));
Adapter adapter = new Adapter(this, mlist);
recList.setAdapter(adapter);
recList.setLayoutManager(new LinearLayoutManager(this));
这是 Logcat 错误
2018-10-19 19:17:20.479 10635-10635/fishingfreaks.ffapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: fishingfreaks.ffapp, PID: 10635
java.lang.RuntimeException: Unable to start activity ComponentInfo{fishingfreaks.ffapp/fishingfreaks.ffapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at fishingfreaks.ffapp.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
【问题讨论】:
-
具体出了什么问题?
-
应用程序在运行时崩溃。
-
崩溃?您是否有一个调试器可以显示错误以及错误发生的位置?它只是默默地退出吗? Stack Overflow 不仅仅是一个调试服务——你需要一个更具体的问题才能得到一个好的答案。
-
我用来自 Logcat 的更多细节更新了这个问题。应用程序静默退出。
-
java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' 你的 logcat已经说明了问题。您的回收站视图为空。而且您正在尝试在该回收站视图上设置 setHasFixedSize(true)。检查你的活动 xml,recyclerview 的 id 就在那里。
标签: java android android-studio android-recyclerview