在Android中将String转换为Integer时出错

时间:2018-10-26 17:57:37

标签: java android string integer

我正在尝试将字符串值转换为int,但它给我错误:

  

android.content.res.resources $ notfoundexception字符串资源ID#0x16 << / p>

我试图像这样解析:

       String date = "29-30-2098";
       String[] d = date.split("-");
       int di = Integer.parseInt(d[1]);
       Toast.makeText(MainActivity.this, di, Toast.LENGTH_SHORT).show();

如果我将String数据类型设置为“ di”,则它将打印正确的值。但是当我尝试将其解析为Int时给出错误 我也尝试过使用ValueOf();,但是在所有方法中它都给我错误。 错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.myanxietyjournal, PID: 8830
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myanxietyjournal/com.myanxietyjournal.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1e
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
                  at android.os.Handler.dispatchMessage(Handler.java:105)
                  at android.os.Looper.loop(Looper.java:156)
                  at android.app.ActivityThread.main(ActivityThread.java:6523)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
               Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1e
                  at android.content.res.HwResources.getText(HwResources.java:442)
                  at android.widget.Toast.makeText(Toast.java:307)
                  at com.myanxietyjournal.MainActivity.onCreate(MainActivity.java:48)
                  at android.app.Activity.performCreate(Activity.java:6915)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864) 
                  at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
                  at android.os.Handler.dispatchMessage(Handler.java:105) 
                  at android.os.Looper.loop(Looper.java:156) 
                  at android.app.ActivityThread.main(ActivityThread.java:6523) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832) 

1 个答案:

答案 0 :(得分:0)

makeText有两种版本:

第一个使用String作为第二个参数,并在字符串中向用户显示消息。

第二个参数以资源ID 作为第二个参数,尝试查找该资源,但由于您没有具有该ID的资源而失败。

要向用户显示int ,您需要先将其转换为字符串,例如String.valueOf(di)