解析数据会导致我的应用崩溃 - 为什么?

时间:2014-04-29 09:27:13

标签: java android

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

        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)

2 个答案:

答案 0 :(得分:0)

与'重载处理器'没什么关系,这是一个数据问题:

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

如果你想让它变得健壮,你的代码需要检查狡猾的数据。

答案 1 :(得分:0)

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

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

Integer.parseInt(string);