【问题标题】:Resources$NotFoundException: String resource ID #0x0 with Toast.makeTextResources$NotFoundException: 带有 Toast.makeText 的字符串资源 ID #0x0
【发布时间】:2023-03-25 21:16:01
【问题描述】:

当我单击 RecyclerView 项目时,我的应用程序崩溃了,由于Toast.make() 而发生错误,当我在check(positon:Int) 函数中评论或删除Toast 时,它不会崩溃。只有在Toast 存在时才会崩溃。我搜索了很多解决方法,但找不到,请帮助解决此错误

fun check(position: Int)
{
   Log.i("Check", position.toString())
   //if i remove the comment the app will crash
   //Toast.makeText(this, position, Toast.LENGTH_SHORT).show()
}

错误:

W/ResourceType: No package identifier when getting value for resource number 0x00000000
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.firebase_image_upload, PID: 27287
    android.content.res.Resources$NotFoundException: String resource ID #0x0
        at android.content.res.Resources.getText(Resources.java:331)
        at android.widget.Toast.makeText(Toast.java:287)
        at com.example.firebase_image_upload.ImagesActivity$check$1.run(ImagesActivity.kt:63)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

【问题讨论】:

    标签: android android-studio kotlin android-toast


    【解决方案1】:

    android.content.res.Resources$NotFoundException: 字符串资源 ID #0x0

    使用Toast.makeText(this, position, Toast.LENGTH_SHORT).show() 你正在尝试使用the method

    public static Toast makeText (Context context, 
                    int resId, 
                    int duration)
    

    其中 resId 表示要使用的字符串资源的资源ID(如R.string.xxxx)

    使用 position.toString() 将 int 转换为 String:

    Toast.makeText(this, position.toString(), Toast.LENGTH_SHORT)
    

    【讨论】:

      【解决方案2】:
      Toast.makeText(ProjectActivity.this,  "Your message here",  Toast.LENGTH_SHORT).show();
      

      第二个参数是String,但是你在这里使用Int,把你的Int转换成String就可以了 例如:

      `Toast.makeText(this, position.toString(), Toast.LENGTH_SHORT).show()`
      
      
      
      Toast.makeText(this, ""+position, Toast.LENGTH_SHORT).show()
      

      【讨论】:

        猜你喜欢
        • 2019-08-16
        • 1970-01-01
        • 2023-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多