java.lang.NullPointerException ListView 中的问题
java.lang.NullPointerException problem in ListView
我是 Android 开发的新手,我正在尝试将字符串转换为双精度数,然后将其四舍五入到小数点后两位,然后将其转换回字符串以显示在 ListView 中。这是我的代码:
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent){
String expenseCategory = getItem(position).getExpenseCategory();
String expenseAmount = getItem(position).getExpenseAmount();
String expenseEntryDate = getItem(position).getExpenseTodayEntry();
Double eA = Double.parseDouble(expenseAmount);
String eAnew = String.format("%.2f",eA);
Expenses ex = new Expenses(eAnew, expenseCategory, expenseEntryDate);
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
TextView expenseCategoryTextView = (TextView) convertView.findViewById(R.id.CategoryHistoryTV);
TextView expenseDateEntryTextView = (TextView) convertView.findViewById(R.id.DateHistoryTV);
TextView expenseAmountTextView = (TextView) convertView.findViewById(R.id.AmountHistoryTV);
expenseCategoryTextView.setText(expenseCategory);
expenseDateEntryTextView.setText(expenseEntryDate);
expenseAmountTextView.setText(eAnew);
expenseCategoryTextView.setTextColor(Color.RED);
expenseDateEntryTextView.setTextColor(Color.RED);
expenseAmountTextView.setTextColor(Color.RED);
return convertView;
}
这里是错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference
它运行正常,没有:
Double eA = Double.parseDouble(expenseAmount);
String eAnew = String.format("%.2f",eA);
但是它给了我一个像
这样的输出
1000.0000
我知道这可能是一个糟糕的设计,但是由于各种原因,将 expenseAmount 声明为 double 在我的代码的其他区域不起作用。
我已经尝试使用一个条件 "catch" 错误提示的空值,但到目前为止没有任何效果。
首先,您没有发布完整的代码,其中您正在从双精度转换为字符串,反之亦然;第二,根据错误 'java.lang.String java.lang.String.trim()',这意味着您调用的字符串为空 trim function.So 在调用这个函数之前确保字符串必须有一些值 trim.
这里好像expenseAmount
是空的。尝试为列表中的项目提供默认 expenseAmount
值或处理 null
案例。
喜欢:
public class Expense { // whatever your class is
private String expenseAmount = "0"; // set a default value, so it's not null
...
}
或者:
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
...
String expenseAmount = getItem(position).getExpenseAmount();
...
String eAnew;
if (expenseAmount == null) {
eAnew = "I don't have an expenseAmount yet!"; // handle the null case
else {
Double eA = Double.parseDouble(expenseAmount);
eAnew = String.format("%.2f", eA);
}
Expenses ex = new Expenses(eAnew, expenseCategory, expenseEntryDate);
...
}
修改您指出问题的代码并在那里添加 Null 检查。下面我已经提到了代码示例。
String eAnew = "00.00";
if(expenseAmount != null && !"null".equals(expenseAmount)){
Double eA = Double.parseDouble(expenseAmount);
eAnew = String.format("%.2f",eA);
}
我是 Android 开发的新手,我正在尝试将字符串转换为双精度数,然后将其四舍五入到小数点后两位,然后将其转换回字符串以显示在 ListView 中。这是我的代码:
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent){
String expenseCategory = getItem(position).getExpenseCategory();
String expenseAmount = getItem(position).getExpenseAmount();
String expenseEntryDate = getItem(position).getExpenseTodayEntry();
Double eA = Double.parseDouble(expenseAmount);
String eAnew = String.format("%.2f",eA);
Expenses ex = new Expenses(eAnew, expenseCategory, expenseEntryDate);
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
TextView expenseCategoryTextView = (TextView) convertView.findViewById(R.id.CategoryHistoryTV);
TextView expenseDateEntryTextView = (TextView) convertView.findViewById(R.id.DateHistoryTV);
TextView expenseAmountTextView = (TextView) convertView.findViewById(R.id.AmountHistoryTV);
expenseCategoryTextView.setText(expenseCategory);
expenseDateEntryTextView.setText(expenseEntryDate);
expenseAmountTextView.setText(eAnew);
expenseCategoryTextView.setTextColor(Color.RED);
expenseDateEntryTextView.setTextColor(Color.RED);
expenseAmountTextView.setTextColor(Color.RED);
return convertView;
}
这里是错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference
它运行正常,没有:
Double eA = Double.parseDouble(expenseAmount);
String eAnew = String.format("%.2f",eA);
但是它给了我一个像
这样的输出1000.0000
我知道这可能是一个糟糕的设计,但是由于各种原因,将 expenseAmount 声明为 double 在我的代码的其他区域不起作用。
我已经尝试使用一个条件 "catch" 错误提示的空值,但到目前为止没有任何效果。
首先,您没有发布完整的代码,其中您正在从双精度转换为字符串,反之亦然;第二,根据错误 'java.lang.String java.lang.String.trim()',这意味着您调用的字符串为空 trim function.So 在调用这个函数之前确保字符串必须有一些值 trim.
这里好像expenseAmount
是空的。尝试为列表中的项目提供默认 expenseAmount
值或处理 null
案例。
喜欢:
public class Expense { // whatever your class is
private String expenseAmount = "0"; // set a default value, so it's not null
...
}
或者:
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
...
String expenseAmount = getItem(position).getExpenseAmount();
...
String eAnew;
if (expenseAmount == null) {
eAnew = "I don't have an expenseAmount yet!"; // handle the null case
else {
Double eA = Double.parseDouble(expenseAmount);
eAnew = String.format("%.2f", eA);
}
Expenses ex = new Expenses(eAnew, expenseCategory, expenseEntryDate);
...
}
修改您指出问题的代码并在那里添加 Null 检查。下面我已经提到了代码示例。
String eAnew = "00.00";
if(expenseAmount != null && !"null".equals(expenseAmount)){
Double eA = Double.parseDouble(expenseAmount);
eAnew = String.format("%.2f",eA);
}