0

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

4
  • 3
    check this Resources$NotFoundException: String resource ID And set Toast.makeText(this, ""+numero, Toast.LENGTH_SHORT).show(); Commented May 16, 2018 at 7:32
  • check if(!nombre.isempty) then convert it to int Commented May 16, 2018 at 7:35
  • 1
    Toast.makeText(this, String.valueOf(numero), Toast.LENGTH_SHORT).show(); Commented May 16, 2018 at 7:38
  • Share your code where you are setting intent data. Commented May 16, 2018 at 7:41

3 Answers 3

3

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();

Sign up to request clarification or add additional context in comments.

6 Comments

As an opinion, String.valueOf(numero); is more efficient and elegant
@MarcEstrada yeah, great idea. Updated the answer
And voted your answer :) there is a lot of lates answer saying same of you, so voted yours
@MarcEstrada Thanks!
@Mimmetico great! Remember to mark answer as approved, so that others can benefit too
|
1

Try this:

 number = Integer.parseInt(YourStringName.toString());

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.