NullPointerException in Java Android App MediaPlayer

后端 未结 3 1758
北荒
北荒 2021-01-26 07:29

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, bu

3条回答
  •  耶瑟儿~
    2021-01-26 08:11

    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();
    
                }
            });
       }
    
    
    }
    

提交回复
热议问题