英文:
java.lang.NumberFormatException: null in onBindViewHolder
问题
这是我翻译的内容:
我正在制作一个聊天应用,但当我点击发送按钮时出现了这个错误...请问有人能解释一下 "null" 是什么意思,以及为什么会出现这个错误吗?因为这不是第一次发生了,这真的让我很烦恼...如果你能帮助我就请帮帮我吧。
public class AdapterChat extends RecyclerView.Adapter<AdapterChat.MyHolder> /* 这段代码是第二个错误 */
{
private static final int MSG_TYPE_LEFT = 0;
private static final int MSG_TYPE_RIGHT = 1;
Context context;
List<Modelchat> chatList;
String imageUrl;
FirebaseUser fUser;
public AdapterChat(Context context, List<Modelchat> chatList, String imageUrl) {
this.context = context;
this.chatList = chatList;
this.imageUrl = imageUrl;
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (i == MSG_TYPE_RIGHT) {
View view = LayoutInflater.from(context).inflate(R.layout.row_chat_right, viewGroup, false);
return new MyHolder(view);
} else {
View view = LayoutInflater.from(context).inflate(R.layout.row_chat_left, viewGroup, false);
return new MyHolder(view);
}
}
@Override
public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
String message = chatList.get(i).getMessage();
String timeStamp = chatList.get(i).getTimestamp();
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(Long.parseLong(timeStamp)); /* 这段代码是第一个错误 */
String dataTime = android.text.format.DateFormat.format("dd/MM/yyyy hh:mm aa", cal).toString();
myHolder.messageTv.setText(message);
myHolder.timeTv.setText(dataTime);
try {
Picasso.get().load(imageUrl).into(myHolder.profileTv);
} catch (Exception e) {
}
if (i == chatList.size()-1) {
if (chatList.get(i).isSeen()) {
myHolder.isSeenTv.setText("已查看");
} else {
myHolder.isSeenTv.setText("已送达");
}
} else {
myHolder.isSeenTv.setVisibility(View.GONE);
}
}
@Override
public int getItemCount() {
return chatList.size();
}
@Override
public int getItemViewType(int position) {
fUser = FirebaseAuth.getInstance().getCurrentUser();
if (chatList.get(position).getSender().equals(fUser.getUid())) {
return MSG_TYPE_RIGHT;
} else {
return MSG_TYPE_LEFT;
}
}
class MyHolder extends RecyclerView.ViewHolder {
ImageView profileTv;
TextView messageTv, timeTv, isSeenTv;
public MyHolder(@NonNull View itemView) {
super(itemView);
profileTv = itemView.findViewById(R.id.profileTv);
messageTv = itemView.findViewById(R.id.messageTv);
timeTv = itemView.findViewById(R.id.timeTv);
isSeenTv = itemView.findViewById(R.id.isSeenTv);
}
}
}
错误信息:
java.lang.NumberFormatException: null
at cm.example.learno.SocialApp.adapters.AdapterChat.onBindViewHolder(AdapterChat.java:56)
at com.example.learno.SocialApp.adapters.AdapterChat.onBindViewHolder(AdapterChat.java:23)
我在错误的代码旁边添加了注释
英文:
I am making a chat app but when I click send button this error occur .. and please can any one explain to me what is meaning of null and why it happens ? because it is not first time and is is a really annoying to me .. please help me if you can..
public class AdapterChat extends RecyclerView.Adapter<AdapterChat.MyHolder> /* this code is second error */
{
private static final int MSG_TYPE_LEFT = 0;
private static final int MSG_TYPE_RIGHT = 1;
Context context;
List<Modelchat> chatList;
String imageUrl;
FirebaseUser fUser;
public AdapterChat(Context context, List<Modelchat> chatList, String imageUrl) {
this.context = context;
this.chatList = chatList;
this.imageUrl = imageUrl;
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (i == MSG_TYPE_RIGHT) {
View view = LayoutInflater.from(context).inflate(R.layout.row_chat_right, viewGroup, false);
return new MyHolder(view);
} else {
View view = LayoutInflater.from(context).inflate(R.layout.row_chat_left, viewGroup, false);
return new MyHolder(view);
}
}
@Override
public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
String message = chatList.get(i).getMessage();
String timeStamp = chatList.get(i).getTimestamp();
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(Long.parseLong(timeStamp)); /* this code is first error*/
String dataTime = android.text.format.DateFormat.format("dd/MM/yyyy hh:mm aa", cal).toString();
myHolder.messageTv.setText(message);
myHolder.timeTv.setText(dataTime);
try {
Picasso.get().load(imageUrl).into(myHolder.profileTv);
} catch (Exception e) {
}
if (i == chatList.size()-1) {
if (chatList.get(i).isSeen()) {
myHolder.isSeenTv.setText("Seen");
} else {
myHolder.isSeenTv.setText("Delivered");
}
} else {
myHolder.isSeenTv.setVisibility(View.GONE);
}
}
@Override
public int getItemCount() {
return chatList.size();
}
@Override
public int getItemViewType(int position) {
fUser = FirebaseAuth.getInstance().getCurrentUser();
if (chatList.get(position).getSender().equals(fUser.getUid())) {
return MSG_TYPE_RIGHT;
} else {
return MSG_TYPE_LEFT;
}
}
class MyHolder extends RecyclerView.ViewHolder {
ImageView profileTv;
TextView messageTv, timeTv, isSeenTv;
public MyHolder(@NonNull View itemView) {
super(itemView);
profileTv = itemView.findViewById(R.id.profileTv);
messageTv = itemView.findViewById(R.id.messageTv);
timeTv = itemView.findViewById(R.id.timeTv);
isSeenTv = itemView.findViewById(R.id.isSeenTv);
}
}
}
Error message
java.lang.NumberFormatException: null
at cm.example.learno.SocialApp.adapters.AdapterChat.onBindViewHolder(AdapterChat.java:56)
at com.example.learno.SocialApp.adapters.AdapterChat.onBindViewHolder(AdapterChat.java:23)
i am made a comment beside the wrong code
答案1
得分: 3
尝试将
String timeStamp = chatList.get(i).getTimestamp();
更改为
String timeStamp = chatList.get(myHolder.getAdapterPosition()).getTimestamp();
您正在尝试解析空值时间戳 timeStamp
到
cal.setTimeInMillis(Long.parseLong(timeStamp));
至于什么是 null
,请花些时间进行研究和阅读。您可以从这里开始。
英文:
try changing
String timeStamp = chatList.get(i).getTimestamp();
to
String timeStamp = chatList.get(myHolder.getAdapterPosition()).getTimestamp();
You are trying to parse null value timeStamp into
cal.setTimeInMillis(Long.parseLong(timeStamp));
As for what null
is, please take the time to research and read on it. You can start here
答案2
得分: 0
使用 DateTimeFormatter 替代 DateFormat,如果您想要同时格式化日期和时间。
另一种修复方法是从格式化程序中移除时间,就像这样 dd/MM/yyyy
。
英文:
Use DateTimeFormatter instead of DateFormat if you want to format date and time simultaneously.
Another way to fix that is remove time from formatter like this dd/MM/yyyy
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论