Home » Android » java – How to position music files from raw to play according to ListView Items-Exceptionshub

java – How to position music files from raw to play according to ListView Items-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

When an item of a ListView is clicked, it then shows the full lyrics texts in the NewActivity.java. In this class NewActivity.java, I set MediaPlayer which draws its music file data from raw folder. The biggest challenge is have here to set the soundfiles in position with listview items and send them to NewActivity using intents for playback.

I want a situation when song001 is clicked to open into NewActivity then I click on the play button to play the soundfile; R.raw.song_1 for song 001

This is what i tried that gave me error
intent.putExtra(“position”, modellist.get(i).soundfile);

Please help me. Thanks

From ListViewAdapter.java

//listview soundfile file for songs in position
    soundfile= new int[] {R.raw.song_1,R.raw.song_2,R.raw.song_3, R.raw.song_4,R.raw.song_5,R.raw.song_6,R.raw.song_7,R.raw.song_8,};

        //listview item clicks
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //code later
                Intent intent = new Intent(mContext, NewActivity.class);
                intent.putExtra("position", modellist.get(i).soundfile);
                intent.putExtra("actionBarTitle", modellist.get(i).getTitle());
                intent.putExtra("brandNewDesc", modellist.get(i).getBrandNewDesc());
                mContext.startActivity(intent);
            }
        });
        return view;
    }

Model Class

public class Model {

    String title;
    String desc;
    int icon;
    int soundfile;
    String brandNewDesc;

    //constructor
    public Model(String title, String desc, String description, int icon, int i) {
        this.title = title;
        this.desc = desc;
        this.icon = icon;
        this.soundfile = soundfile;
        this.brandNewDesc = description;
    }

    //getters
    public String getTitle() {
        return title;
    }

    public String getDesc() {
        return desc;
    }

    public String getBrandNewDesc(){

        return brandNewDesc;
    }

    public int getIcon() {
        return icon;
    }

    public int getSoundfile() {
        return soundfile;
    }
}

LogCat below

2020-02-21 10:22:53.531 7908-7908/com.gritchen.redeemedsongs E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.gritchen.redeemedsongs, PID: 7908
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
        at android.view.View.performClick(View.java:6748)
        at android.view.View$PerformClick.run(View.java:25458)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6549)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
        at android.view.View.performClick(View.java:6748) 
        at android.view.View$PerformClick.run(View.java:25458) 
        at android.os.Handler.handleCallback(Handler.java:790) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6549) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888) 
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
        at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:220)
        at android.content.res.ResourcesImpl.openRawResourceFd(ResourcesImpl.java:317)
        at android.content.res.Resources.openRawResourceFd(Resources.java:1293)
        at android.media.MediaPlayer.create(MediaPlayer.java:980)
        at android.media.MediaPlayer.create(MediaPlayer.java:963)
        at com.gritchen.redeemedsongs.NewActivity.play(NewActivity.java:89)
        at java.lang.reflect.Method.invoke(Native Method) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384) 
        at android.view.View.performClick(View.java:6748) 
        at android.view.View$PerformClick.run(View.java:25458) 
        at android.os.Handler.handleCallback(Handler.java:790) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6549) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888) 
How to&Answers:

In this intent.putExtra("position", modellist.get(i).soundfile); your soundfile is not declared as public in your Model class. Also you don’t reall need to as it for this purpose you have the public getter function getSoundfile().

So use that instead as below and see if it solves your issue.

intent.putExtra("position", modellist.get(i).getSoundfile());

If this still doesn’t solve your issue post what the error you are getting is so we can understand the issue.