1. 程式人生 > >Caused by: java.lang.StringIndexOutOfBoundsException: length=5; regionStart=0; regionLength=-1

Caused by: java.lang.StringIndexOutOfBoundsException: length=5; regionStart=0; regionLength=-1

Caused by: java.lang.StringIndexOutOfBoundsException: length=5; regionStart=0; regionLength=-1
原因:在擷取字串時,取到了負值:String temp_m = c_mount.substring(0,w);
提示的資訊是,這個字串c_mount的長度是5,從0開始,取到了-1,所以報錯了。


應該查詢的地方是w,後來發現int w=c_mount.lastIndexOf(getText


(R.string.gram).toString());
分析了一下,發現c_mount是從資料庫中取出來的,很可能是資料庫中的資料的問題


,開啟資料庫,檢視,發現果然,這個欄位不全是相同的單位,有克,有毫升,所以


,分開對待就好了。問題解決。