【发布时间】: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(); -
在您设置意图数据的地方分享您的代码。