解决:Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
Posted 一只假喵
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决:Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2相关的知识,希望对你有一定的参考价值。
在运行android app时出现以下错误:
--------- beginning of crash 11-28 07:53:56.485 6712-6712/com.trojane.android.learnactivity E/AndroidRuntime: FATAL EXCEPTION: main Process: com.trojane.android.learnactivity, PID: 6712 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.trojane.android.learnactivity/com.trojane.android.learnactivity.SecondActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x2 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2 at android.content.res.Resources.getText(Resources.java:348) at android.widget.Toast.makeText(Toast.java:307) at com.trojane.android.learnactivity.SecondActivity.onCreate(SecondActivity.java:19) at android.app.Activity.performCreate(Activity.java:7136) at android.app.Activity.performCreate(Activity.java:7127) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
而出错的代码是:
Toast.makeText(SecondActivity.this,getIntent().getIntExtra("initNum",1),Toast.LENGTH_LONG).show()
那么我们就看一下 makeText 方法的源代码:
public static Toast makeText(Context context, CharSequence text, @Duration int duration) { return makeText(context, null, text, duration); }
可以看到该方法中第二个参数的类型是 CharSequence 类型的, 那么CharSequence 是什么类型呢?我们可以看下图:
从上面这个图可以看出 CharSequence 是个接口类型,他的子类有我们熟悉的char、String、StringBuffer 等类型,应该说所有的字符类型都是直接或者间接实现了这个接口,
所以,对于makeText的第二个参数只能传入一个字符型。
原因: makeText 中第二个参数只能是CharSequence的直接或间接实现类,而传入的 int 类型的被认为是String Resource ID
解决方法:将 int 类型数值改成 String或其他字符型
以上是关于解决:Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2的主要内容,如果未能解决你的问题,请参考以下文章
Spring循环依赖问题如何解决?Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException
解决:Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
报错 Caused by: java.lang.ClassNotFoundException: javax.wsdl.OperationType的解决方式
报错 Caused by: java.lang.ClassNotFoundException: javax.wsdl.OperationType的解决方式
报错Caused by: java.lang.ClassNotFoundException: javax.xml.soap.SOAPException的解决方式
报错Caused by: java.lang.ClassNotFoundException: javax.xml.soap.SOAPException的解决方式