简体   繁体   中英

Error while converting String to Integer in android

I am trying to convert string value into int but it give me error:

android.content.res.resources$notfoundexception string resource id #0x16 <

I tried parsing like:

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

if i give String data type to "di" then it print the correct value. But when i try to parse it to Int it give the error And i also tried by using ValueOf(); but in all methods it give me error. Error:

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) 

There are two versions of makeText :

The first one takes a String as the second parameter, and shows the message in the string to the user.

The second one takes a resource id as the second parameter, tries to lookup the resource, and fails because you don't have a resource with that id.

To show the int value to the user, you need to convert it to string first, eg String.valueOf(di) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM