2014-04-29 51 views
-1

因此,我正在製作一個應用程序,將網頁中的數據作爲文本,將其解析爲數據結構,然後將其顯示在表格中。麻煩的是,我編寫了創建數據結構並將數據放置在表中的代碼,但現在,當我運行它時,該應用程序崩潰。這裏是相關的代碼:解析數據使我的應用程序崩潰 - 爲什麼?

 protected void onPostExecute(String result) { 
     String s1 = makeReadable(result); 
     ArrayList <OfferRequest> al = makeStructures(s1); 
     makeRows(al); 
    } 
} 

public String makeReadable(String input){ 
    String result = input.replace("~~", "[empty]"); 
    String result2 = result.replace("~", ""); 
    String result3 = result2.replace("<", ""); 
    String result4 = result3.replace(">", ""); 
    return result4; 
} 

public ArrayList <OfferRequest> makeStructures(String input){ 
    OfferRequest or; 
    ArrayList <OfferRequest> orList = new ArrayList <OfferRequest>(); 
    String [] items = input.split ("\n"); 
    String [] splitItems; 
    for(int i = 0; i < items.length; i++){ 
     splitItems = items[i].split("|"); 
     int id = Integer.parseInt(splitItems[0]); 
     int isOffer = Integer.parseInt(splitItems[1]); 
     boolean isOffer2 = (isOffer != 0); 
     int units = Integer.parseInt(splitItems[2]); 
     double price = Double.parseDouble(splitItems[3]); 
     String currency = splitItems[4]; 
     String created = splitItems[5]; 
     String specs = splitItems[6]; 
     String comment = splitItems[7]; 
     String companyName = splitItems[8]; 
     String category = splitItems[9]; 
     String itemName = splitItems[10]; 
     String user = splitItems[11]; 
     String toBeDelivered = splitItems[12]; 
     String lastUpdated = splitItems[13]; 
     or = new OfferRequest(id, isOffer2, units, price, currency, created, specs, comment, companyName, category, itemName, user, toBeDelivered, lastUpdated); 
     orList.add(or); 
    } 
    return orList; 
} 

public void makeRows(ArrayList <OfferRequest> input){ 
    TableRow t; 
    TextView t1, t2, t3, t4, t5, t6, t7, t8; 
    for (int i = 0; i < input.size(); i++){ 
     t1 = new TextView(this); 
     t2 = new TextView(this); 
     t3 = new TextView(this); 
     t4 = new TextView(this); 
     t5 = new TextView(this); 
     t6 = new TextView(this); 
     t7 = new TextView(this); 
     t8 = new TextView(this); 
     t1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     t2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     t3.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     t4.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     t5.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     t6.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     t7.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     t8.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     t1.setText(input.get(i).getCategory()); 
     t2.setText(input.get(i).getItemName()); 
     t3.setText(Integer.toString(input.get(i).getUnits())); 
     t4.setText(input.get(i).getCurrency()); 
     t5.setText(Double.toString(input.get(i).getPrice())); 
     t6.setText(input.get(i).getUser()); 
     t7.setText("Unknown"); 
     t8.setText(input.get(i).getCreated()); 
     t = new TableRow(this); 
     t.addView(t1); 
     t.addView(t2); 
     t.addView(t3); 
     t.addView(t4); 
     t.addView(t5); 
     t.addView(t6); 
     t.addView(t7); 
     t.addView(t8); 
     table.addView(t); 
    } 
} 

我只是超載設備的處理器或什麼?當我剛把它打印在表格中的未格式化數據時,該應用程序運行緩慢,儘管緩慢。

這裏的logcat的東西:

04-29 12:11:37.570: E/AndroidRuntime(1763): FATAL EXCEPTION: main 
04-29 12:11:37.570: E/AndroidRuntime(1763): java.lang.NumberFormatException: Invalid int: "" 
04-29 12:11:37.570: E/AndroidRuntime(1763):  at java.lang.Integer.invalidInt(Integer.java:138) 
04-29 12:11:37.570: E/AndroidRuntime(1763):  at java.lang.Integer.parseInt(Integer.java:359) 
04-29 12:11:37.570: E/AndroidRuntime(1763):  at java.lang.Integer.parseInt(Integer.java:332) 
04-29 12:11:37.570: E/AndroidRuntime(1763):  at com.onegdd.orbit.MainActivity.makeStructures(MainActivity.java:263) 
04-29 12:11:37.570: E/AndroidRuntime(1763):  at com.onegdd.orbit.MainActivity.makeReadable(MainActivity.java:253) 
04-29 12:11:37.570: E/AndroidRuntime(1763):  at com.onegdd.orbit.MainActivity$DownloadWebpageTask.onPostExecute(MainActivity.java:203) 
04-29 12:11:37.570: E/AndroidRuntime(1763):  at com.onegdd.orbit.MainActivity$DownloadWebpageTask.onPostExecute(MainActivity.java:1) 
04-29 12:11:37.570: E/AndroidRuntime(1763):  at android.os.AsyncTask.finish(AsyncTask.java:631) 
04-29 12:11:37.570: E/AndroidRuntime(1763):  at android.os.AsyncTask.access$600(AsyncTask.java:177) 
04-29 12:11:37.570: E/AndroidRuntime(1763):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644) 
04-29 12:11:37.570: E/AndroidRuntime(1763):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-29 12:11:37.570: E/AndroidRuntime(1763):  at android.os.Looper.loop(Looper.java:137) 
04-29 12:11:37.570: E/AndroidRuntime(1763):  at android.app.ActivityThread.main(ActivityThread.java:4895) 
04-29 12:11:37.570: E/AndroidRuntime(1763):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-29 12:11:37.570: E/AndroidRuntime(1763):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-29 12:11:37.570: E/AndroidRuntime(1763):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994) 
04-29 12:11:37.570: E/AndroidRuntime(1763):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) 
04-29 12:11:37.570: E/AndroidRuntime(1763):  at dalvik.system.NativeStart.main(Native Method) 
+1

發表您的logcat的。 – duggu

+0

「應用程序崩潰」非常模糊,我們沒有任何線索發生了什麼。通過發佈exceptions/logcat/ect來澄清發生了什麼問題... – csmckelvey

+0

並在'Integer.parseInt'之前檢查空值。如果被分析的值爲空,那可能會導致問題。 –

回答

0

無關,與「超載處理器」,這是一個數據問題:

12:11:37.570: E/AndroidRuntime(1763): java.lang.NumberFormatException: Invalid int: "" 

你的代碼將需要檢查狡猾的數據,如果你希望它是健壯的。

+0

好的,謝謝。還有一件事 - 有沒有辦法找出錯誤是指什麼? Logcat似乎沒有說什麼數據是無效的。 –

+0

棘手,因爲你沒有太多錯誤發生的地方(例如行號)。你的'makeStructures'方法中有循環,所以可能在周圍添加'try..catch'塊來記錄異常和索引計數器,這可能有幫助嗎? – stridecolossus

+0

我會試試。謝謝! –

0

是你越來越... java.lang.NumberFormatException

嘗試這樣,如果數字是不是INT格式...

Integer.parseInt(string); 
相關問題