在运行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)
那么我们就看一下 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或其他字符型
原文地址:https://www.cnblogs.com/trojane/p/10032682.html
时间: 2024-12-29 01:42:01