android無法轉換字串到整型

來源:互聯網
上載者:User

問題描述:

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

[java]
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()); 
    } 

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());
    }我還測試了:

[java]
nteger.valueOf(str);  

Integer.valueOf(str);
得到的錯誤:

[java]
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 

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()
你改測試就簡單的好好測,看如下

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

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下定義一個

[java]
<string name="my_str">abc123456</string> 

<string name="my_str">abc123456</string>
然後這樣去得到它:

[java]
Log.i("s", getString(R.string.my_str));//s abc123456 

Log.i("s", getString(R.string.my_str));//s abc123456

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.