应用力下降suddently [英] App forces down suddently

查看:218
本文介绍了应用力下降suddently的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个音乐app.the的应用程序有16个音乐btns.the应用程序时没有问题,但我preSS的btns运行多次应用力下降。

  super.onCreate(冰柱);
        的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};            对于(INT I:IDS){
                B =(按钮)findViewById(ⅰ);
                b.setOnClickListener(本);
            }}
      //的onCreate以外的()
        @覆盖
        公共无效的onClick(视图v){            开关(v.getId()){
                案例R.id.btn:
                     如果(熔点=空&放大器;!&放大器; mp.isPlaying())mp.stop();
                    MP = MediaPlayer.create(zoo.this,R.raw.gata);
                    mp.start();
                    打破;

这是code和我使用的情况下,每btn.When应用程序势力下来,logcat的在mp.start找到一个NullPointerException异常();中强制应用down..please帮助按钮!

编辑下面从评论:

 情况下R.id.btn:
    如果(熔点=空&放大器;!&放大器; mp.isPlaying())mp.stop();
    mp.reset();
    尝试{
        mp.setDataSource(zoo.this,R.raw.gata);
    }赶上(抛出:IllegalArgumentException E1){
        // TODO自动生成catch块
        e1.printStackTrace();
    }赶上(E1 IllegalStateException异常){
        // TODO自动生成catch块
        e1.printStackTrace();
    }赶上(IOException异常E1){
        // TODO自动生成catch块
        e1.printStackTrace();
    }
    mp.start();
    打破;


解决方案

我觉得一点就是MediaPlayer的是pretty重量级的资源,你不应该创建太多了。另外,只要你用它做,调用它的release()方法。匿名的点的都是有效的:你应该尝试重用你的媒体播放,而不是创建一个新的,你应该非常熟悉的MediaPlayer的文档。例如从MediaPlayer的文档:


  

资源可包括单
  资源,例如硬件
  加速度分量和不
  呼叫释放()可能会导致后续
  MediaPlayer的对象的实例
  回退到软件实现
  或完全失败。


的假设是,你分配大量的MediaPlayer对象和/或不释放他们的速度不够快。然而,如果没有更多的code这是不可能的肯定。

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;

解决方案

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.

这篇关于应用力下降suddently的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆