用一些机器人在音板上强制关闭问题?

时间:2011-02-10 19:33:45

标签: android button sdk crash

好吧,我的问题是,当我打开应用程序时,我试图点击某些歌曲时强行关闭。这很奇怪,它在Galaxy Tab上运行良好但不适用于Galaxy S,机器人2,充满活力等等。以下是我用于此事的代码:

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;



public class MyMain extends Activity {


    /** Called when the activity is first created. */
    MediaPlayer mpButtonClick;
    MediaPlayer pButtonClick;
    MediaPlayer oButtonClick;
    MediaPlayer tButtonClick;
    MediaPlayer sButtonClick;
    MediaPlayer aButtonClick;
    MediaPlayer dButtonClick;
    MediaPlayer cButtonClick;
    MediaPlayer lButtonClick;
    MediaPlayer himButtonClick;
    MediaPlayer herButtonClick;
    MediaPlayer hellButtonClick;
    MediaPlayer mocButtonClick;
    MediaPlayer rasButtonClick;
    MediaPlayer raseButtonClick;
    MediaPlayer dalekButtonClick;
    MediaPlayer sharButtonClick;
    MediaPlayer godButtonClick;


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

      //set up the button sounds
  mpButtonClick = MediaPlayer.create(this, R.raw.money);

        Button bmoney = (Button) findViewById(R.id.money);
        bmoney.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mpButtonClick.start();

            }
        });


      pButtonClick = MediaPlayer.create(this, R.raw.pants);

        Button bpants = (Button) findViewById(R.id.pants);
        bpants.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                pButtonClick.start();

            }       


        });

   oButtonClick = MediaPlayer.create(this, R.raw.oath);

        Button oath = (Button) findViewById(R.id.oath);
        oath.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                oButtonClick.start();

            }       


        });

     tButtonClick = MediaPlayer.create(this, R.raw.tardis);

        Button tardis = (Button) findViewById(R.id.tardis);
        tardis.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                tButtonClick.start();

            }       


        });

     sButtonClick = MediaPlayer.create(this, R.raw.screwdriver);

        Button screwdriver = (Button) findViewById(R.id.screwdriver);
        screwdriver.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                sButtonClick.start();

            }       


        });

      aButtonClick = MediaPlayer.create(this, R.raw.alarm);

        Button alarm = (Button) findViewById(R.id.alarm);
        alarm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                aButtonClick.start();

            }       


        });

     dButtonClick = MediaPlayer.create(this, R.raw.kame);

        Button kame = (Button) findViewById(R.id.kame);
        kame.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dButtonClick.start();

            }       

            });

     cButtonClick = MediaPlayer.create(this, R.raw.card);

        Button card = (Button) findViewById(R.id.card);
        card.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                cButtonClick.start();

            }       

            });

    lButtonClick = MediaPlayer.create(this, R.raw.laugh);

        Button laugh = (Button) findViewById(R.id.laugh);
        laugh.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                lButtonClick.start();

            }       

            });

himButtonClick = MediaPlayer.create(this, R.raw.him);

        Button him = (Button) findViewById(R.id.him);
        him.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                himButtonClick.start();

            }       

            });

herButtonClick = MediaPlayer.create(this, R.raw.her);

        Button her = (Button) findViewById(R.id.her);
        her.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                herButtonClick.start();

            }       

            });

hellButtonClick = MediaPlayer.create(this, R.raw.hell);

        Button hell = (Button) findViewById(R.id.hell);
        hell.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                hellButtonClick.start();

            }       

            });

mocButtonClick = MediaPlayer.create(this, R.raw.mocking);

        Button mocking = (Button) findViewById(R.id.mocking);
        mocking.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mocButtonClick.start();

            }       

            });

godButtonClick = MediaPlayer.create(this, R.raw.god);

        Button god = (Button) findViewById(R.id.god);
        god.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                godButtonClick.start();

            }       

            });

rasButtonClick = MediaPlayer.create(this, R.raw.rasengan);

        Button rasengan = (Button) findViewById(R.id.rasengan);
        rasengan.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                rasButtonClick.start();

            }       

            });

raseButtonClick = MediaPlayer.create(this, R.raw.rasen);

        Button rasen = (Button) findViewById(R.id.rasen);
        rasen.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                raseButtonClick.start();

            }       

            });

dalekButtonClick = MediaPlayer.create(this, R.raw.dalek);

        Button dalek = (Button) findViewById(R.id.dalek);
        dalek.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dalekButtonClick.start();

            }       

            });

sharButtonClick = MediaPlayer.create(this, R.raw.sharingan);

        Button sharingan = (Button) findViewById(R.id.sharingan);
        sharingan.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                sharButtonClick.start();

            }       

            });




    }


}

对不起我需要帮助的长代码

编辑:如果有帮助我总是得到:

显示java.lang.NullPointerException MyMain $ 8.onClick()

1 个答案:

答案 0 :(得分:2)

没有让logcat输出或堆栈跟踪工作,我的猜测是你通过一次将多个MediaPlayer个实例加载到内存中,让大多数设备都在尖叫!调用MediaPlayer.create()不仅会创建另一个实例,还会在其上调用prepare()并准备好滚动它。也许尝试一种更简化的方法:

public class MyMain extends Activity implements MediaPlayer.OnCompletionListener {

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

    //Set up the buttons
    Button bmoney = (Button) findViewById(R.id.money);
    bmoney.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playSound(R.raw.money);
        }
    });

    Button bpants = (Button) findViewById(R.id.pants);
    bpants.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playSound(R.raw.pants);
        }       
    });

    Button oath = (Button) findViewById(R.id.oath);
    oath.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playSound(R.raw.oath);
        }       
    });

    Button tardis = (Button) findViewById(R.id.tardis);
    tardis.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playSound(R.raw.tardis);
        }       
    });

    Button screwdriver = (Button) findViewById(R.id.screwdriver);
    screwdriver.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playSound(R.raw.screwdriver);
        }       
    });

    Button alarm = (Button) findViewById(R.id.alarm);
    alarm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playSound(R.raw.alarm);
        }       
    });

    Button kame = (Button) findViewById(R.id.kame);
    kame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playSound(R.raw.kame);
        }       
    });

    Button card = (Button) findViewById(R.id.card);
    card.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playSound(R.raw.card);
        }       

    });

    Button laugh = (Button) findViewById(R.id.laugh);
    laugh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playSound(R.raw.laugh);
        }       
    });

    Button him = (Button) findViewById(R.id.him);
    him.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playSound(R.raw.him);
        }       
    });

    Button her = (Button) findViewById(R.id.her);
    her.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playSound(R.raw.her);
        }       
    });

    Button hell = (Button) findViewById(R.id.hell);
    hell.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playSound(R.raw.hell);
        }       
    });

    Button mocking = (Button) findViewById(R.id.mocking);
    mocking.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playSound(R.raw.mocking);
        }       
    });

    Button god = (Button) findViewById(R.id.god);
    god.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playSound(R.raw.god);
        }       
    });

    Button rasengan = (Button) findViewById(R.id.rasengan);
    rasengan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playSound(R.raw.rasengan);
        }       
    });

    Button rasen = (Button) findViewById(R.id.rasen);
    rasen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playSound(R.raw.rasen);
        }       
    });

    Button dalek = (Button) findViewById(R.id.dalek);
    dalek.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playSound(R.raw.dalek);
        }       
    });

    Button sharingan = (Button) findViewById(R.id.sharingan);
    sharingan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playSound(R.raw.sharingan);
        }       
    });
}

//Consolidate media playing to one location
private void playSound(int resId) {
    MediaPlayer mp = MediaPlayer.create(this, resId);
    mp.setOnCompletionListener(this);
    mp.start();
}

//Completion listener method
public void onCompletion(MediaPlayer mp) {
    mp.release();
}

}

此方法将创建MediaPlayer实例,并仅在您需要时播放声音。一旦完成,监听器就可以释放资源以保持合理的内存占用。

您还可以通过在XML中使用android:onClick使所有按钮调用一种方法,然后使用基于按钮ID的正确ID调用playSound()来进一步简化此代码(除非您的定位1.5,当然)。

希望有所帮助!