程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-android無法轉換字符串到整型

java-android無法轉換字符串到整型

編輯:編程綜合問答
android無法轉換字符串到整型

求助:

從一個XML文件中獲取字符串,內容大概就是262626這樣。現在需要將字符串轉換到 MB (/1024) 形式。但是不能轉換int或者長整型。我的測試代碼:

String size = parser.getValue(e, parser.KEY_FileSizeInByte);
Log.i("menuItems",size);

    try {
        int s = Integer.parseInt(size);
        Log.i("s", getString(s));
    }
    catch (Exception e1) {
        Log.i("error", e1.getMessage());
    }

我還測試了:

Integer.valueOf(str); 

得到的錯誤:

04-15 23:06:17.652: I/menuItems(6819): 97035
04-15 23:06:17.652: W/ResourceType(6819): No package identifier when getting value for resource number 0x00017b0b
04-15 23:06:17.652: I/error(6819): String resource ID #0x17b0b
04-15 23:06:17.692: I/menuItems(6819): 2957
04-15 23:06:17.692: W/ResourceType(6819): No package identifier when getting value for resource number 0x00000b8d
04-15 23:06:17.692: I/error(6819): String resource ID #0xb8d
04-15 23:06:17.692: I/menuItems(6819): 559711
04-15 23:06:17.692: W/ResourceType(6819): No package identifier when getting value for resource number 0x00088a5f
04-15 23:06:17.692: I/error(6819): String resource ID #0x88a5f
04-15 23:06:17.692: I/menuItems(6819): 153651
04-15 23:06:17.692: W/ResourceType(6819): No package identifier when getting value for resource number 0x00025833
04-15 23:06:17.692: I/error(6819): String resource ID #0x25833
04-15 23:06:17.692: I/menuItems(6819): 1733630
04-15 23:06:17.692: W/ResourceType(6819): No package identifier when getting value for resource number 0x001a73fe
04-15 23:06:17.692: I/error(6819): String resource ID #0x1a73fe
04-15 23:06:17.692: I/menuItems(6819): 532105
04-15 23:06:17.692: W/ResourceType(6819): No package identifier when getting value for resource number 0x00081e89
04-15 23:06:17.692: I/error(6819): String resource ID #0x81e89
04-15 23:06:17.702: I/menuItems(6819): 28817
04-15 23:06:17.702: W/ResourceType(6819): No package identifier when getting value for resource number 0x00007091
04-15 23:06:17.702: I/error(6819): String resource ID #0x7091
04-15 23:06:17.702: I/menuItems(6819): 90787
04-15 23:06:17.702: W/ResourceType(6819): No package identifier when getting value for resource number 0x000162a3
04-15 23:06:17.702: I/error(6819): String resource ID #0x162a3
04-15 23:06:17.702: I/menuItems(6819): 33556
04-15 23:06:17.702: W/ResourceType(6819): No package identifier when getting value for resource number 0x00008314
04-15 23:06:17.702: I/error(6819): String resource ID #0x8314
04-15 23:06:17.702: I/menuItems(6819): 122145
04-15 23:06:17.702: W/ResourceType(6819): No package identifier when getting value for resource number 0x0001dd21
04-15 23:06:17.702: I/error(6819): String resource ID #0x1dd21
04-15 23:06:17.702: I/menuItems(6819): 323312
04-15 23:06:17.702: W/ResourceType(6819): No package identifier when getting value for resource number 0x0004eef0
04-15 23:06:17.702: I/error(6819): String resource ID #0x4eef0
04-15 23:06:17.702: I/menuItems(6819): 532676
04-15 23:06:17.702: W/ResourceType(6819): No package identifier when getting value for resource number 0x000820c4
04-15 23:06:17.702: I/error(6819): String resource ID #0x820c4
04-15 23:06:17.712: I/menuItems(6819): 49133
04-15 23:06:17.712: W/ResourceType(6819): No package identifier when getting value for resource number 0x0000bfed
04-15 23:06:17.712: I/error(6819): String resource ID #0xbfed
04-15 23:06:17.712: I/menuItems(6819): 622507
04-15 23:06:17.712: W/ResourceType(6819): No package identifier when getting value for resource number 0x00097fab
04-15 23:06:17.712: I/error(6819): String resource ID #0x97fab
04-15 23:06:17.712: I/menuItems(6819): 57333
04-15 23:06:17.712: W/ResourceType(6819): No package identifier when getting value for resource number 0x0000dff5
04-15 23:06:17.712: I/error(6819): String resource ID #0xdff5
04-15 23:06:17.712: I/menuItems(6819): 17265
04-15 23:06:17.712: W/ResourceType(6819): No package identifier when getting value for resource number 0x00004371
04-15 23:06:17.712: I/error(6819): String resource ID #0x4371

最佳回答:


報這個warning的不是你的轉換,而是你接下來的這句getString()
你改測試就簡單的好好測,看如下

try {
            int s = Integer.parseInt(size);
        System.out.println("Got num is:"+s);//正確
            //Log.i("s", getString(s));
        .....

關於getString()的用法解釋是:
Return a localized string from the application's package's default string table.
你可以在values下的strings.xml下定義一個

<string name="my_str">abc123456</string>

然後這樣去得到它:

Log.i("s", getString(R.string.my_str));//s abc123456
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved