How to fix: Resources$NotFoundException while calling Toast

0

I wanted to display the Selecteditemposition (number of clicked item in a Listview) in a Toast when an item is clicked in my Listview, but any method in this event crashes.

This is the crash report:

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)

And this is the code:

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

What am I doing wrong?

java
android
listview
asked on Stack Overflow Jun 27, 2019 by Creesch 2.0 • edited Jun 27, 2019 by Reaz Murshed

2 Answers

3

lineNumber is an integer, so you are using this overload of makeText():

makeText(Context context, int resId, int duration)

Which expects an integer string resource id, so lineNumber is considered a string resource id, which of course is not so it can't be found.

Instead what you should be doing is convert lineNumber to string and use this overload of makeText():

makeText(Context context, CharSequence text, int duration)

So do this:

Toast.makeText(MainActivity.this, String.valueOf(lineNumber), Toast.LENGTH_SHORT).show();
answered on Stack Overflow Jun 27, 2019 by forpas • edited Jun 27, 2019 by Reaz Murshed
0

So linenumber is already returned onclick method as position.

Toast.maketext(getApplicationContext,position.toString(), Toast.LENGHT_LONG).show();
answered on Stack Overflow Jun 27, 2019 by user11710521 • edited Jun 27, 2019 by Tamir Abutbul

User contributions licensed under CC BY-SA 3.0