[Solved] Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2

The following error occurred while running 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)

The error code is:

Toast.makeText(SecondActivity.this,getIntent().getIntExtra("initNum",1),Toast.LENGTH_LONG).show()

Let’s take a look at the source code of the maketext method

public static Toast makeText(Context context, CharSequence text, @Duration int duration) {
        return makeText(context, null, text, duration);
    }

You can see that the type of the second parameter in this method is charsequence. What is the charsequence type?We can see the following figure:

As can be seen from the above figure, charsequence is an interface type. Its subclasses include char, string, StringBuffer and other familiar types. It should be said that all character types directly or indirectly implement this interface

Therefore, only one character type can be passed in for the second parameter of maketext

Reason: the second parameter in maketext can only be the direct or indirect implementation class of charsequence, and the passed in int type is considered as string resource ID

Solution: change the value of int type to string or other character type

Similar Posts: