如何解决ListView音乐文件中的ResourceNotFoundException

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何解决ListView音乐文件中的ResourceNotFoundException相关的知识,希望对你有一定的参考价值。

我正在尝试将原始文件夹中的音乐文件设置为在打开NewActivity的listView中播放。我已经配置了Model类,MainActivity和ListviewAdapter,但仍然收到错误。也许我在某处弄错了。请帮我解决。感谢您的宝贵时间。

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 music) 
        this.title = title;
        this.desc = desc;
        this.icon = icon;
        this.soundfile = music;
        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;
    

来自MainActivity.java

 MusicFile= 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 icons for song titles in position
        icon = new int[] R.drawable.song, R.drawable.song, R.drawable.song, R.drawable.song,R.drawable.song,R.drawable.song, R.drawable.song,R.drawable.song,;

        listView = findViewById(R.id.list);

        for (int i = 0; i< title.size(); i++)
            Model model =new Model(title.get(i), description.get(i), newDesc.get(i), icon[i], MusicFile[i]);
            //bind all strings in an array
            arrayList.add(model);
        

        //pass result to listview class
        adapter = new ListViewAdapter(this, arrayList);

        //bind the adapter to the listview class
        listView.setAdapter(adapter);

来自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("actionBarTitle", modellist.get(i).getTitle());
                intent.putExtra("brandNewDesc", modellist.get(i).getBrandNewDesc());
                intent.putExtra("soundfile", modellist.get(i).getSoundfile());
                mContext.startActivity(intent);


            
        );


        return view;

NewActivity.java

// setting up media players


    public void  play(View v) 
        if (player == null) 
            //then here, I sent the position of the chosen song in the intent extras.
            //the get back the extra
            int position = 0;
            try
                position=getIntent().getIntExtra("soundfile",0);
            catch (Exception e)
            
                e.printStackTrace();
            

            String fileToPlay="song_"+position;




            player = MediaPlayer.create(this, getResources().getIdentifier(fileToPlay,"raw",getPackageName()));
            Toast.makeText(this, "Hymn Tune Playing", Toast.LENGTH_SHORT ).show();

这是我的LogCat

 Process: com.gritchen.redeemedsongs, PID: 28671
    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) 
答案

正如我所看到的,您将资源标识符放入了用于“ soundfile”键的意图中,但是从那里获取它时应该期望位置。尝试像这样创建MediaPlayer

MediaPlayer.create(this, position)

以上是关于如何解决ListView音乐文件中的ResourceNotFoundException的主要内容,如果未能解决你的问题,请参考以下文章

ListView 元素背景 - 资源可变()

如何将资产文件夹中的本地 JSON 文件解析为 ListView?

如何在 Visual Basic 6 ListView 上进行多列排序?

如何在 Flutter 的 ListView 中添加列?

android中扫描模拟器sd卡中mp3后缀的所有音乐文件,并显示到listView中,求源码或实现方法?

如何导出iPhone/iPad中QQ音乐文件