我有一個功能齊全的應用程序,它接受用戶輸入並將其保存到數據庫中。點擊「日誌」按鈕後,信息顯示在列表視圖中。我創建了一個for循環來保存我的一個列的總數。列名是「grandTotal」,這是我在夜間提出的一些提示。一切都很好,直到我意外地點擊保存按鈕而沒有輸入任何內容,現在它崩潰了,我無法弄清楚如何找到解決方法。任何幫助,將不勝感激!我的代碼如下:Android應用程序在向數據庫輸入值時關閉
@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)
也許我會找出被保存我的數據庫,在那裏,並刪除我無意中保存的條目並刪除它。從現在開始,只要確保我實際上在保存價值,但如果有辦法解決這個問題,我一定會很感激這些知識。
請閱讀例外...這是不言自明的。 – Selvin