0
votes

It should be something easy to do, but these commands return error when I try to convert a String to integer:

        Intent recibir = getIntent();
        String nombre = recibir.getStringExtra("hora_inicio");

        int numero=0;

        try {
            numero = Integer.parseInt(nombre);
        } catch(NumberFormatException nfe) {
            nfe.printStackTrace();
        }

        Toast.makeText(this, numero, Toast.LENGTH_SHORT).show();

Android monitor returns this error:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.cristobal.policlinica, PID: 11025 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cristobal.policlinica/com.example.cristobal.policlinica.CalendarActivity}: android.content.res.Resources$NotFoundException: String resource ID 0x9 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: android.content.res.Resources$NotFoundException: String resource 0x9 at android.content.res.Resources.getText(Resources.java:312) at android.widget.Toast.makeText(Toast.java:286) at com.example.cristobal.policlinica.CalendarActivity.onCreate(CalendarActivity.java:31) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

thanks in advance

3
check this Resources$NotFoundException: String resource ID And set Toast.makeText(this, ""+numero, Toast.LENGTH_SHORT).show();IntelliJ Amiya
check if(!nombre.isempty) then convert it to intLearning Always
Toast.makeText(this, String.valueOf(numero), Toast.LENGTH_SHORT).show();Kush
Share your code where you are setting intent data.buzzingsilently

3 Answers

3
votes

The problem is not with your conversion but with your Toast.makeText. When you call Toast.makeText(this, numero, Toast.LENGTH_SHORT).show();

It considers numero as resId and tries to find the relative string resource in strings.xml which fails giving error:

Resources$NotFoundException: String resource ID

Instead of

Toast.makeText(this, numero, Toast.LENGTH_SHORT).show();

DO

Toast.makeText(this, String.valueOf(numero), Toast.LENGTH_SHORT).show();

1
votes

Try this:

 number = Integer.parseInt(YourStringName.toString());
1
votes
Toast.makeText(this, String.valueOf(numero), Toast.LENGTH_SHORT).show();