Problem getting method to identify which button was clicked

All your problems with Audio, Video and Images.

Problem getting method to identify which button was clicked

Postby CitizenSmif » Sun May 01, 2011 9:34 pm

**Please move to other coding problems or layout - I now know this isn't a multimedia specific**

HI,

I have some code which plays an audio file. It was playing fine before but since I changed the way buttons are generated it now doesn't work (though the call is still inside an onClick method)

Heres my code for the button and to play (I've tried having the play method in my main class but this doesn't work either)

Code: Select all
package com.tuts.working;

import android.media.MediaPlayer;
import android.view.View;
import android.view.View.OnClickListener;

class MyOnClickListener extends soundboard implements OnClickListener {

    private final int position; 
    
    public MyOnClickListener(int position) 
    { 
     this.position = position; 
    } 
      
   @Override
   public void onClick(View v) {
      play();
   }
   
    public void play() {
         urii = R.raw.lul;
         MediaPlayer mp = MediaPlayer.create(this, urii);

         mp.start();
}
}


I get the following error:

Code: Select all
05-01 20:12:33.289: ERROR/AndroidRuntime(804): Uncaught handler: thread main exiting due to uncaught exception
05-01 20:12:33.299: ERROR/AndroidRuntime(804): java.lang.NullPointerException
05-01 20:12:33.299: ERROR/AndroidRuntime(804):     at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
05-01 20:12:33.299: ERROR/AndroidRuntime(804):     at android.media.MediaPlayer.create(MediaPlayer.java:560)
05-01 20:12:33.299: ERROR/AndroidRuntime(804):     at com.tuts.working.MyOnClickListener.play(MyOnClickListener.java:18)
05-01 20:12:33.299: ERROR/AndroidRuntime(804):     at com.tuts.working.MyOnClickListener.onClick(MyOnClickListener.java:38)
05-01 20:12:33.299: ERROR/AndroidRuntime(804):     at android.view.View.performClick(View.java:2344)
05-01 20:12:33.299: ERROR/AndroidRuntime(804):     at android.view.View.onTouchEvent(View.java:4133)
05-01 20:12:33.299: ERROR/AndroidRuntime(804):     at android.widget.TextView.onTouchEvent(TextView.java:6510)
05-01 20:12:33.299: ERROR/AndroidRuntime(804):     at android.view.View.dispatchTouchEvent(View.java:3672)
05-01 20:12:33.299: ERROR/AndroidRuntime(804):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
05-01 20:12:33.299: ERROR/AndroidRuntime(804):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
05-01 20:12:33.299: ERROR/AndroidRuntime(804):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
05-01 20:12:33.299: ERROR/AndroidRuntime(804):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
05-01 20:12:33.299: ERROR/AndroidRuntime(804):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)


I've tried using an ogg and a mp3 file but neither is working anymore. I can't understand why this isn't working at all now. Does anybody know why this might be happening?
Last edited by CitizenSmif on Mon May 02, 2011 12:01 am, edited 1 time in total.
CitizenSmif
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Feb 17, 2011 12:45 am

Top

Re: Sound file doesn't play anymore?

Postby CitizenSmif » Sun May 01, 2011 10:36 pm

OK, this isn't actually a media player problem (please can a mod move)

I've tried just displaying a toast message onClick but this results in the same error.

The buttons generate perfectly, it's only once the button is clicked there is a problem which I assume is a problem with the button being selected. I've tried adding

Code: Select all
   public void onClick(View v) {
      play(this.position);
   }


This doesn't work either.

I have an onClickListener that uses a variable 'position' that tracks buttons, that is passed to the click listener

How would I display a different (i.e) toast message on the every button generated in the gridview?

I've tried

Code: Select all
   @Override
   public void onClick(View v) {
      function(this.position);
   }
   
    private void function(int position) {
      if (position == 1) {
         Toast.makeText(this, "test", Toast.LENGTH_SHORT).show();
      } else {
         Toast.makeText(this, "test2", Toast.LENGTH_SHORT).show();
      }
}


Still crashes with the getResources() error.. Any help would be much appreciated :x
CitizenSmif
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Feb 17, 2011 12:45 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 13 guests