【问题标题】:Why cannot convert from string to int?为什么不能从字符串转换为int?
【发布时间】:2018-10-26 03:35:44
【问题描述】:

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

        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: 致命异常: main 进程:com.example.cristobal.policlinica,PID:11025 java.lang.RuntimeException:无法启动活动 组件信息{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:字符串 资源 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) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

提前致谢

【问题讨论】:

  • 检查此Resources$NotFoundException: String resource ID 并设置Toast.makeText(this, ""+numero, Toast.LENGTH_SHORT).show();
  • 检查 if(!nombre.isempty) 然后将其转换为 int
  • Toast.makeText(this, String.valueOf(numero), Toast.LENGTH_SHORT).show();
  • 在您设置意图数据的地方分享您的代码。

标签: android string int


【解决方案1】:

问题不在于您的转换,而在于您的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();

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

【讨论】:

  • 作为意见,String.valueOf(numero);更高效优雅
  • @MarcEstrada 是的,好主意。更新了答案
  • 投了你的票 :) 有很多迟到的回答说你也是这样,所以投了你的票
  • @MarcEstrada 谢谢!
  • @Mimmetico 太棒了!记得将答案标记为已批准,以便其他人也可以受益
【解决方案2】:

试试这个:

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

【讨论】:

    【解决方案3】:
    Toast.makeText(this, String.valueOf(numero), Toast.LENGTH_SHORT).show();
    

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-22
      • 2012-10-17
      • 2021-04-11
      相关资源
      最近更新 更多