我想在Selecteditemposition中单击某个项目时在Listview中显示Toast(在Listview中单击的项目数),但是此事件中的任何方法都会崩溃。

这是当机报告:

2019-06-27 20:54:02.681 13006-13015/? E/ample.basicipt: Failed to send DDMS packet REAQ to debugger (-1 of 20): Broken pipe
2019-06-27 20:54:17.470 13006-13006/com.example.basiciptv E/ample.basicipt: No package ID ff found for ID 0xffffffff.
2019-06-27 20:54:17.472 13006-13006/com.example.basiciptv E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.basiciptv, PID: 13006
    android.content.res.Resources$NotFoundException: String resource ID #0xffffffff
        at android.content.res.Resources.getText(Resources.java:348)
        at android.widget.Toast.makeText(Toast.java:307)
        at com.example.basiciptv.MainActivity$4.onItemClick(MainActivity.java:336)
        at android.widget.AdapterView.performItemClick(AdapterView.java:318)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1192)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3169)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6692)
        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)


这是代码:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
        boolean co = true;
        int lineNumber = listview.getSelectedItemPosition();
        Toast.makeText(MainActivity.this, lineNumber, Toast.LENGTH_SHORT).show();
    }
});


我究竟做错了什么?

最佳答案

lineNumber是整数,因此您正在使用makeText()的此重载:

makeText(Context context, int resId, int duration)


它需要一个整数的字符串资源ID,因此lineNumber被认为是一个字符串资源ID,当然不是,因此无法找到它。

相反,您应该做的是将lineNumber转换为字符串并使用makeText()的此重载:

makeText(Context context, CharSequence text, int duration)


这样做:

Toast.makeText(MainActivity.this, String.valueOf(lineNumber), Toast.LENGTH_SHORT).show();

关于java - 如何解决:调用Toast时Resources $ NotFoundException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56797734/

10-09 00:00