Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android無法轉換字符串到整型

android無法轉換字符串到整型

編輯:關於Android編程

問題描述:

從一個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

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved