英文:
Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference android studio
问题
以下是翻译好的内容:
我正在尝试使用Android Studio构建一个应用程序。我正在按照一个教程进行操作,但问题是,那个人在暂停屏幕以启动模拟器时,当他再次播放时,有一段代码丢失了。我不确定他确切做了什么,但我尝试了我所有可以找到的选项。
我遇到了这个错误:
java.lang.NullPointerException: 尝试调用空对象引用上的 'int java.util.ArrayList.size()' 的虚拟方法
代码:
public void onResponse(Call<Users> call, Response<Users> response) {
if (response.isSuccessful()) {
mediaObjectList = (ArrayList<MediaObject>) response.body().getAllPosts();
recyclerview.setMediaObjects(mediaObjectList);
VideoPlayerRecyclerAdapter adapter = new VideoPlayerRecyclerAdapter(mediaObjectList, initGlide());
recyclerview.setAdapter(adapter);
adapter.notifyDataSetChanged();
recyclerview.setKeepScreenOn(true);
recyclerview.smoothScrollToPosition(mediaObjectList.size()+1);
} else {
Toast.makeText(HomeActivity.this, "网络错误。", Toast.LENGTH_SHORT).show();
}
}
我在这一行代码上遇到了错误:
recyclerview.smoothScrollToPosition(mediaObjectList.size()+1);
英文:
I am trying to build an app with android studio. I am following a tutorial but the thing is, the guy paused the screen to start the emulator and when he played it again a piece of code was missing. I don't know for sure what he did but I tried all of the options presented to me.
I am getting this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
code:
public void onResponse(Call<Users> call, Response<Users> response) {
if (response.isSuccessful()) {
mediaObjectList = (ArrayList<MediaObject>) response.body().getAllPosts();
recyclerview.setMediaObjects(mediaObjectList);
VideoPlayerRecyclerAdapter adapter = new VideoPlayerRecyclerAdapter(mediaObjectList, initGlide());
recyclerview.setAdapter(adapter);
adapter.notifyDataSetChanged();
recyclerview.setKeepScreenOn(true);
recyclerview.smoothScrollToPosition(mediaObjectList.size()+1);
} else {
Toast.makeText(HomeActivity.this, "Network Error.", Toast.LENGTH_SHORT).show();
}
}
I'm getting the error on this line:
recyclerview.smoothScrollToPosition(mediaObjectList.size()+1);
答案1
得分: 1
检查是否`getAllPost()`有值或为空
***正确方式:***
public void onResponse(Call<Users> call, Response<Users> response) {
if (response.isSuccessful()) {
mediaObjectList = (ArrayList<MediaObject>) response.body().getAllPosts();
if (mediaObjectList != null) {
recyclerview.setMediaObjects(mediaObjectList);
VideoPlayerRecyclerAdapter adapter = new VideoPlayerRecyclerAdapter(mediaObjectList, initGlide());
recyclerview.setAdapter(adapter);
adapter.notifyDataSetChanged();
recyclerview.setKeepScreenOn(true);
recyclerview.smoothScrollToPosition(mediaObjectList.size() + 1);
} else {
Toast.makeText(HomeActivity.this, "没有可用的帖子。", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(HomeActivity.this, "网络错误。", Toast.LENGTH_SHORT).show();
}
}
英文:
Check if getAllPost()
has value or not
Correct Way:
public void onResponse(Call<Users> call, Response<Users> response) {
if (response.isSuccessful()) {
mediaObjectList = (ArrayList<MediaObject>) response . body ().getAllPosts();
if (mediaObjectList != null) {
recyclerview.setMediaObjects(mediaObjectList);
VideoPlayerRecyclerAdapter adapter = new VideoPlayerRecyclerAdapter(mediaObjectList, initGlide());
recyclerview.setAdapter(adapter);
adapter.notifyDataSetChanged();
recyclerview.setKeepScreenOn(true);
recyclerview.smoothScrollToPosition(mediaObjectList.size() + 1);
} else {
Toast.makeText(HomeActivity.this, "No Post Available.", Toast.LENGTH_SHORT).show();
}
}
else {
Toast.makeText(HomeActivity.this, "Network Error.", Toast.LENGTH_SHORT).show();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论