Android 为什么不能从字符串转换为int?

Android 为什么不能从字符串转换为int?,android,string,int,Android,String,Int,这应该很容易做到,但当我尝试将字符串转换为整数时,这些命令返回错误: Intent recibir = getIntent(); String nombre = recibir.getStringExtra("hora_inicio"); int numero=0; try { numero = Integer.parseInt(nombre); } catch(NumberFormatE

这应该很容易做到,但当我尝试将字符串转换为整数时,这些命令返回错误:

        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返回此错误:

E/AndroidRuntime:致命异常:主 流程:com.example.cristobal.polilinica,PID:11025 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.cristobal.policlinica/com.example.cristobal.policlinica.CalendarActivity}: android.content.res.Resources$NotFoundException:字符串资源ID 0x9 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 位于android.app.ActivityThread.-wrap11(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 位于android.os.Handler.dispatchMessage(Handler.java:102) 位于android.os.Looper.loop(Looper.java:148) 位于android.app.ActivityThread.main(ActivityThread.java:5417) 位于java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 原因:android.content.res.Resources$NotFoundException:String 资源0x9 位于android.content.res.Resources.getText(Resources.java:312) 位于android.widget.Toast.makeText(Toast.java:286) 在 com.example.cristobal.polilinica.CalendarActivity.onCreate(CalendarActivity.java:31) 位于android.app.Activity.performCreate(Activity.java:6237) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 位于android.app.ActivityThread.-wrap11(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 位于android.os.Handler.dispatchMessage(Handler.java:102) 位于android.os.Looper.loop(Looper.java:148) 位于android.app.ActivityThread.main(ActivityThread.java:5417) 位于java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


提前感谢

问题不在于您的转换,而在于您的
Toast.makeText
。调用
Toast.makeText(this,numero,Toast.LENGTH_SHORT).show()时

它将
numero
视为resId,并尝试在
strings.xml
中查找相对字符串资源,但未能给出错误:

Resources$NotFoundException:字符串资源ID

而不是

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

DO


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

问题不在于转换,而在于您的
Toast.makeText
。调用
Toast.makeText(this,numero,Toast.LENGTH_SHORT).show()时

它将
numero
视为resId,并尝试在
strings.xml
中查找相对字符串资源,但未能给出错误:

Resources$NotFoundException:字符串资源ID

而不是

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

DO

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