我有一个功能齐全的应用程序,它接受用户输入并将其保存到数据库中。单击“日志”按钮后,信息将显示在列表视图中。我创建了一个for循环来保持我的一个列的运行总计。列名是“grandTotal”,这是我为夜晚制作的提示总计。一切都工作得很好,直到我不小心点击保存按钮而没有输入任何东西,现在它崩溃了,我无法弄清楚如何找到解决方法。任何帮助,将不胜感激!我的代码如下:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.contact_entry, null);
TextView name= (TextView) convertView.findViewById(R.id.contactName);
TextView contact = (TextView) convertView.findViewById(R.id.contact);
TextView address = (TextView) convertView.findViewById(R.id.addressConverted);
TextView orderTotal = (TextView) convertView.findViewById(R.id.orderTotalConverted);
TextView amountReceived = (TextView) convertView.findViewById(R.id.amountReceivedConverted);
TextView tip = (TextView) convertView.findViewById(R.id.tipConverted);
TextView mileage = (TextView) convertView.findViewById(R.id.mileageConverted);
TextView grandTotal = (TextView) convertView.findViewById(R.id.grandTotalConverted);
Button delete = (Button) convertView.findViewById(R.id.delete);
PhoneBook list = contactList.get(position);
name.setText(list.getName());
contact.setText(list.getPhoneNumber());
address.setText(list.getAddress());
orderTotal.setText(list.getOrderTotal());
amountReceived.setText(list.getAmountReceived());
tip.setText(list.getTip());
mileage.setText(list.getMileage());
grandTotal.setText(list.getGrandTotal());
delete.setOnClickListener(new ListItemClickListener(position, list));
TextView deliveryNumber = (TextView) convertView.findViewById(R.id.deliveryNum);
deliveryNumber.setText("Delivery # " + (position + 1));
TextView runningTotal = (TextView) convertView.findViewById(R.id.runningTotal);
float sum = 0;
for (int i = 0; i <= position; i++)
{
sum = Float.valueOf(list.getGrandTotal()) + sum;
}
runningTotal.setText("Running Total: " + sum);
return convertView;
}
我得到的错误来自我的for循环中的这行代码:
sum = Float.valueOf(list.getGrandTotal()) + sum;
就像我说的那样,只要我实际上将数据保存到数据库,应用程序就运行得很好,但是当我保存(没有)时,它最终崩溃并且从那时起就没有运行。
我得到的logcat错误是:
FATAL EXCEPTION: main
Process: com.example.boley.databaseexample, PID: 23988
java.lang.NumberFormatException: Invalid float: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseFloat(StringToReal.java:308)
at java.lang.Float.parseFloat(Float.java:306)
at java.lang.Float.valueOf(Float.java:343)
at com.example.boley.databaseexample.PhonebookAdapter.getView(PhonebookAdapter.java:86)
at android.widget.AbsListView.obtainView(AbsListView.java:2929)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1305)
at android.widget.ListView.onMeasure(ListView.java:1212)
at android.view.View.measure(View.java:20158)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1111)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:631)
at android.view.View.measure(View.java:20158)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:20158)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:20158)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20158)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:20158)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158)
at android.view.View.measure(View.java:20158)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2644)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1599)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1891)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1487)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7467)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
at android.view.Choreographer.doCallbacks(Choreographer.java:695)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
也许我必须找出我的数据库保存位置并删除我意外保存并删除它的条目。从现在开始,只要确保我实际上是在保存价值,但是如果有办法解决这个问题,我肯定会欣赏这些知识。
答案 0 :(得分:1)
当我保存(没有)时,它最终崩溃了
所以我认为你的list.getGrandTotal()
没有任何价值或者有空字符串而导致NumberFormatException
所以在解析为Float之前检查:
String value = list.getGrandTotal();
if (value != null && value.length() > 0) {
sum = Float.valueOf(value) + sum;
}