Resources $ NotFoundException:具有Toast.makeText的字符串资源ID#0x0

时间:2019-09-25 18:05:40

标签: android android-studio kotlin android-toast

当我单击RecyclerView项时,我的应用程序崩溃了,由于Toast.make()出现了错误,当我在Toast函数中注释或删除了check(positon:Int)时,它没有崩溃。仅当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)

2 个答案:

答案 0 :(得分:4)

  

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转换为字符串:

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

答案 1 :(得分:1)

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()