Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

android - App forces down suddently

i have created a music app.the app has 16 music btns.the app is running with no problem but as i press the btns many times the app forces down..

     super.onCreate(icicle);
        setContentView(R.layout.main);  
        int[] ids = {R.id.btn,R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9, R.id.btn10,
                R.id.btn11, R.id.btn12, R.id.btn13, R.id.btn14, R.id.btn15, R.id.btn16 };



            for (int i : ids) {
                b =  (Button) findViewById(i);
                b.setOnClickListener(this);
            }}
      //outside of onCreate()
        @Override
        public void onClick(View v) {

            switch(v.getId()) {
                case R.id.btn:
                     if (mp != null && mp.isPlaying()) mp.stop();
                    mp = MediaPlayer.create(zoo.this, R.raw.gata);
                    mp.start();
                    break;

this is the code and i use case for every btn.When the app forces down, the logCat is finding a NullPointerException in the mp.start(); of the button that forces the app down..please help!

EDIT in from comment below:

case R.id.btn: 
    if (mp != null && mp.isPlaying()) mp.stop(); 
    mp.reset(); 
    try { 
        mp.setDataSource("zoo.this,R.raw.gata"); 
    } catch (IllegalArgumentException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
    } catch (IllegalStateException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
    } catch (IOException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
    }
    mp.start(); 
    break;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think the point is that a MediaPlayer is a pretty heavy weight resource and you should not create too many of them. Also, as soon as you are done with it, call it's release() method. Anon's point's are both valid: you should try to reuse your media play instead of creating a new one and you should become very familiar with the MediaPlayer documentation. For instance from the MediaPlayer documentation:

Resource may include singleton resources such as hardware acceleration components and failure to call release() may cause subsequent instances of MediaPlayer objects to fallback to software implementations or fail altogether.

The hypothesis is that you are allocating lots of MediaPlayer objects and/or not releasing them fast enough. However, without more code it's impossible to be certain.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...