Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android MediaPlayer在for循环中播放名称_Android - Fatal编程技术网

Android MediaPlayer在for循环中播放名称

Android MediaPlayer在for循环中播放名称,android,Android,我有10个声音在我的项目名为sound1,sound2。。。。我想根据循环的索引在for循环中创建它们。有什么办法吗?这是我尝试过的: for(int i = 0; i < this.whiteTiles.length ; i++) { this.whiteTiles[i] = new PianoTile(context, 0, R.raw.sound + "i"); this.blackTiles[i] = new Pian

我有10个声音在我的项目名为sound1,sound2。。。。我想根据循环的索引在for循环中创建它们。有什么办法吗?这是我尝试过的:

for(int i = 0; i < this.whiteTiles.length ; i++)
        {
            this.whiteTiles[i] = new PianoTile(context, 0, R.raw.sound + "i");
            this.blackTiles[i] = new PianoTile(context, 1, );
        }   }
}
for(int i=0;i
您的for循环是正确的,但不能使用
R.raw.sound+“i”
,因为:

R.raw.sound
将返回一个int(例如:0x7f040000),连接“i”并返回一个字符串。您将引用生成的
R.java
类中不存在的资源

您应该按照以下步骤进行操作:

Resources res = getResources();

for(int i = 0; i < this.whiteTiles.length ; i++)
{
    int soundId = res.getIdentifier("sound" + i, "raw", getContext().getPackageName());
    this.whiteTiles[i] = new PianoTile(context, 0, soundId);
    this.blackTiles[i] = new PianoTile(context, 1, );
}   
Resources res=getResources();
for(int i=0;i
谢谢,看起来不错,但我遇到了一个错误:“res无法解决”。将上下文引用传递给getResources:Ex:getApplicationContext().getResources();