gpt4 book ai didi

java.lang.NumberFormatException?

转载 作者:行者123 更新时间:2023-12-01 21:53:38 25 4
gpt4 key购买 nike

我是 Android 编程新手。我在聊天应用程序中遇到问题。当向任何用户发送消息时,用户消息存储在 Firebase 数据库中。但是当我尝试显示特定用户的所有聊天时,它给了我一个错误。

问题是 java.lang.NumberFormatException: null

这是我的代码,其中出现错误:

@Override
public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {

//get data
String message = chatList.get(i).getMessage();
String timestamp = chatList.get(i).getTimestamp();

//convert time stamp to dd/mm/yyyy hr:mm am/pm
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(Long.parseLong(timestamp));
//cal.setTimeInMillis(Long.parseLong(timestamp));
String dateTime = android.text.format.DateFormat.format("dd/MM/yyyy hh:mm aa", cal).toString();

//set Data
myHolder.messageTv.setText(message);
myHolder.timeTv.setText(dateTime);

//msg delivered/seen
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);
}

}

enter image description here

最佳答案

使用try-catch block 处理NumberFormatException

Calendar cal = Calendar.getInstance(Locale.ENGLISH);

try {
cal.setTimeInMillis(Long.parseLong(timestamp));
} catch(Exception ex) {
ex.printStackTrace();
}

String dateTime = android.text.format.DateFormat.format("dd/MM/yyyy hh:mm aa", cal).toString();

关于java.lang.NumberFormatException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58753974/

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