NullPointerException in Java Android App MediaPlay

2020-05-02 18:56发布

HELP please this is just a simple android app I am developing, it's meant to play a sound every time you click the button....it works when I click the button at a slow pace, but always crashes if I click the button at a fast pace due to a runtime error - NullPointerException!.....I don't know what I am doing wrong.

THIS IS MY CODE

public class Main extends Activity implements OnClickListener{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button e = (Button) findViewById(R.id.Button1);
    e.setOnClickListener(this);    

}

public void onClick(View v){

    if(v.getId()==R.id.imageButton1){
    final MediaPlayer i = MediaPlayer.create(Main.this, R.raw.sound);
    i.setOnCompletionListener(new OnCompletionListener() {

        public void onCompletion(MediaPlayer mp) {
            i.release();                    
        }
    });

    i.start();  
    }       

}

}

3条回答
对你真心纯属浪费
2楼-- · 2020-05-02 19:33

I had the same problem

my solution was to re-encode the mp3 files with the LAME mp3 encoder

You can use this program: http://www.freac.org/index.php/en/downloads-mainmenu-33

apparently android is quite picky about mp3 codecs

查看更多
等我变得足够好
3楼-- · 2020-05-02 19:37

Try it this way....

public class Blah extends Activity implements OnClickListener {
   MediaPlayer mp;

   @Override 
   public void onCreate(Bundle b)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      Button e = (Button) findViewById(R.id.Button1);

      mp = MediaPlayer.create(R.raw.match);
      mp.setOnCompletionListener(new OnCompletionListener() {

        public void onCompletion(MediaPlayer mp) {
            i.release();                    
        }
    });

      e.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                   mp.seekTo(0);
                       mp.start();

            }
        });
   }


}
查看更多
来,给爷笑一个
4楼-- · 2020-05-02 19:45

Its only a guess but in the documentation here you will find the following

public static MediaPlayer create (Context context, int resid) ... Returns a MediaPlayer object, or null if creation failed

This means you should make sure that the line

final MediaPlayer i = MediaPlayer.create(Main.this, R.raw.sound);

does not return a null.

Edit:

Im really not sure how to handle the situation if create(Context context, int resid) is failing. But you have to do, because it is even from the documentation very clear that it can happen.

To be sure this is really the problem just ignore this case and return from your handler. For example...

public void onClick(View v)
{
    if(v.getId() == R.id.imageButton1)
    {
        final MediaPlayer i = MediaPlayer.create(Main.this, R.raw.sound);

        if (i != null)
        {
            ...

            i.start();
        }
    }       
}
查看更多
登录 后发表回答