-1

I'm new to programming and have been racking my brain for the past couple of hours trying to resolve the issue. I know it's something stupid. I am trying to get each button to play a sound. Any help is greatly appreciated. (ADDED "import android.view.View.OnClickListener;", TURNED MY PROBLEMS GREEN UNTIL I CLOSED IT OUT WITH ";" BUT THEN THE IMPORT GRAYED OUT.)

public class PocketParent extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final MediaPlayer stopMP = MediaPlayer.create(this, R.raw.btmg);
        final MediaPlayer dropMP = MediaPlayer.create(this, R.raw.knock);
        final MediaPlayer popMP = MediaPlayer.create(this, R.raw.oldsoulwr);
        final MediaPlayer noMP = MediaPlayer.create(this, R.raw.smpr);
        final MediaPlayer yeahMP = MediaPlayer.create(this, R.raw.upbeaty);

        ImageButton playstop = (ImageButton) this.findViewById(R.id.imageButton);
        ImageButton playdrop = (ImageButton) this.findViewById(R.id.imageButton2);
        ImageButton playpop = (ImageButton) this.findViewById(R.id.imageButton3);
        ImageButton playno = (ImageButton) this.findViewById(R.id.imageButton4);
        ImageButton playyeah = (ImageButton) this.findViewById(R.id.imageButton5);

        // (HERE IS WHERE MY PROBLEM LIES)
        // *OnClickListener is abstract, cannot be instantiated.*
        // (IT ALSO SAYS IT'S EXPECTING A "," OR " ' " RIGHT BEFORE "new",
        // but that doesn't work either.)

        playstop.setOnClickListener(new View.OnClickListener()
        playdrop.setOnClickListener(new View.OnClickListener()
        playpop.setOnClickListener(new View.OnClickListener()
        playno.setOnClickListener(new View.OnClickListener()
        playyeah.setOnClickListener(new View.OnClickListener()      {
            @Override
            public void onClick(View v) {
                stopMP.start();
                dropMP.start();
                popMP.start();
                noMP.start();
                yeahMP.start();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        }
         ....
    }
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161

2 Answers2

3

You just can't do this:

playstop.setOnClickListener(new View.OnClickListener()
playdrop.setOnClickListener(new View.OnClickListener()
playpop.setOnClickListener(new View.OnClickListener()
....

because this means in java, you are passing to playstop method many arguments with no comma spliting,

It would make more sense syntaxically to do

playstop.setOnClickListener(new View.OnClickListener());
playdrop.setOnClickListener(new View.OnClickListener());
playpop.setOnClickListener(new View.OnClickListener());
....

But you can't do this either because View.OnClickListener is an interface And creating an object of the View.OnClickListener just make no sense...

you need to instead implement anonymously that interface on all that buttons like you did it with the playYeah button

playyeah.setOnClickListener(new View.OnClickListener()      {
        @Override
        public void onClick(View v) {
            .... 
        }
        });
 playstop.setOnClickListener(new View.OnClickListener()      {
        @Override
        public void onClick(View v) {
            .... 
        }
        });
 playdrop.setOnClickListener(new View.OnClickListener()      {
        @Override
        public void onClick(View v) {
            .... 
        }
        });
 Etc etc
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
-2

Try Change

ImageButton playstop = (ImageButton) this.findViewById(R.id.imageButton);
ImageButton playdrop = (ImageButton) this.findViewById(R.id.imageButton2);
ImageButton playpop = (ImageButton) this.findViewById(R.id.imageButton3);
ImageButton playno = (ImageButton) this.findViewById(R.id.imageButton4);
ImageButton playyeah = (ImageButton) this.findViewById(R.id.imageButton5);

to

ImageButton playstop = (ImageButton) findViewById(R.id.imageButton);
ImageButton playdrop = (ImageButton) findViewById(R.id.imageButton2);
ImageButton playpop = (ImageButton) findViewById(R.id.imageButton3);
ImageButton playno = (ImageButton) findViewById(R.id.imageButton4);
ImageButton playyeah = (ImageButton) findViewById(R.id.imageButton5);

Hope It help you!!

Nguyễn Trung Hiếu
  • 2,004
  • 1
  • 10
  • 22