Java 解析数据使我的应用程序崩溃——为什么?

Java 解析数据使我的应用程序崩溃——为什么?,java,android,Java,Android,因此,我正在制作一个应用程序,它将网页中的数据作为文本,解析为数据结构,然后将其显示在表格中。问题是,我编写了创建数据结构并将数据放在表中的代码,但现在,当我运行应用程序时,它崩溃了。以下是相关代码: protected void onPostExecute(String result) { String s1 = makeReadable(result); ArrayList <OfferRequest> al = makeStruc

因此,我正在制作一个应用程序,它将网页中的数据作为文本,解析为数据结构,然后将其显示在表格中。问题是,我编写了创建数据结构并将数据放在表中的代码,但现在,当我运行应用程序时,它崩溃了。以下是相关代码:

        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);
    }
}

与“处理器过载”无关,这是一个数据问题:

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

您的代码需要检查不可靠的数据,如果您想让它变得健壮。

是的,您得到了
java.lang.NumberFormatException

如果数字不是整数格式,请这样尝试

Integer.parseInt(string);

“应用程序崩溃”是如此模糊,我们不知道发生了什么。通过发布异常/logcat/etc…并在
Integer.parseInt
之前检查空值,来澄清问题所在。如果正在解析的值为null,则可能会导致问题。好的,谢谢。不过还有一件事——有没有办法找出错误指的是什么?Logcat似乎没有说明哪些数据无效。这很棘手,因为您没有太多关于错误发生位置的上下文(例如行号)。您的
makeStructures
方法中确实有循环,因此可能需要在其周围添加一个
try..catch
块来记录异常和索引计数器,这可能会有所帮助?我会尝试一下。谢谢
Integer.parseInt(string);