为什么无法从字符串转换为int?

时间:2018-05-16 07:31:00

标签: android string int

这应该是容易的事情,但是当我尝试将String转换为整数时,这些命令会返回错误:

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

  

E / AndroidRuntime:致命异常:主要                                                                                      过程:com.example.cristobal.policlinica,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)                                                                                          at java.lang.reflect.Method.invoke(Native Method)                                                                                          在   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.policlinica.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)                                                                                          at java.lang.reflect.Method.invoke(Native Method)                                                                                          在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

提前致谢

3 个答案:

答案 0 :(得分:3)

问题不在于您的转化,而在于您的Toast.makeText。当您致电Toast.makeText(this, numero, Toast.LENGTH_SHORT).show();

它将numero视为resId并尝试在strings.xml中找到失败的相对字符串资源:

Resources$NotFoundException: String resource ID

而不是

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

<强> DO

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

答案 1 :(得分:1)

$("#btn").click(function(){

    if($("#div1").css('display') == 'block'){
      $("#div1").fadeOut(1000,"linear",function(){         
            alert("Fade Out");
        });
    }
    else{
        $("#div1").fadeIn({
            duration: 1000,
            easing: "linear",
            complete: function(){
                alert("Fade In");
            }
        });
    }
})

答案 2 :(得分:1)

试试这个:

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