1

I was trying to make a simple app with 8 buttons and what it should do is to output an audio file whenever a button is pressed. This was working fine at first every button was assigned with a single onClick method. But the app always crashes when I run it after entering this code:

String  ourId = view.getResources().getResourceEntryName(id);
int resourceId = getResources().getIdentifier(ourId,"raw","com.roostech.gridlayout");

MediaPlayer mplayer  = MediaPlayer.create(this, resourceId);

This is the code:

package com.roostech.gridlayout;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
    public void buttontapped(View view){
        int id = view.getId();

        String  ourId = view.getResources().getResourceEntryName(id);
        int resourceId = getResources().getIdentifier(ourId, "raw", "com.roostech.gridlayout");

        MediaPlayer mplayer  = MediaPlayer.create(this, resourceId);
        Log.i("button tapped", ourId);
    }
}

logcat Image

dferenc
  • 7,918
  • 12
  • 41
  • 49
Abdullah Awan
  • 11
  • 1
  • 3

0 Answers0