因此,我正在製作一個應用程序,將網頁中的數據作爲文本,將其解析爲數據結構,然後將其顯示在表格中。麻煩的是,我編寫了創建數據結構並將數據放置在表中的代碼,但現在,當我運行它時,該應用程序崩潰。這裏是相關的代碼:解析數據使我的應用程序崩潰 - 爲什麼?
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)
發表您的logcat的。 – duggu
「應用程序崩潰」非常模糊,我們沒有任何線索發生了什麼。通過發佈exceptions/logcat/ect來澄清發生了什麼問題... – csmckelvey
並在'Integer.parseInt'之前檢查空值。如果被分析的值爲空,那可能會導致問題。 –