Onclick事件导致随机FC

时间:2012-03-02 20:56:21

标签: java android

我一直在研究一个简单的应用程序,在点击按钮时播放随机声音字节,几乎所有内容都正常工作,但由于某种原因,应用程序强制在点击事件后随机关闭,我无法弄清楚是什么引起这个问题,因为我对android编程还是比较新的。非常感谢任何帮助!

我的代码:

public class Soundboard extends Activity {
    private SoundManager mSoundManager;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // FULLSCREEN CODE
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);

        mSoundManager = new SoundManager();
        mSoundManager.initSounds(getBaseContext());
        mSoundManager.addSound(0, R.raw.sound1);
        mSoundManager.addSound(1, R.raw.sound2);
        mSoundManager.addSound(2, R.raw.sound3);
        mSoundManager.addSound(3, R.raw.sound4);
        mSoundManager.addSound(4, R.raw.sound5);
        mSoundManager.addSound(5, R.raw.sound6);
        mSoundManager.addSound(6, R.raw.sound7);
        mSoundManager.addSound(7, R.raw.sound8);
        mSoundManager.addSound(8, R.raw.sound9);
        mSoundManager.addSound(9, R.raw.sound10);

        ImageButton SoundButton1 = (ImageButton)findViewById(R.id.Btn_Push);
        SoundButton1.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

        Random r = new Random();
        int x = r.nextInt(11);
        mSoundManager.playSound(x);

    }
});
}
}

logcat的:

W/dalvikvm(1146): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime(1146): FATAL EXCEPTION: main
E/AndroidRuntime(1146): java.lang.NullPointerException
E/AndroidRuntime(1146):     at com.tmapps.twss.SoundManager.playSound(SoundManager.java:37)
E/AndroidRuntime(1146):     at com.tmapps.twss.Soundboard$1.onClick(Soundboard.java:49)
E/AndroidRuntime(1146):     at android.view.View.performClick(View.java:2485)
E/AndroidRuntime(1146):     at android.view.View$PerformClick.run(View.java:9080)
E/AndroidRuntime(1146):     at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(1146):     at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(1146):     at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(1146):     at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(1146):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1146):     at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(1146):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(1146):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(1146):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

嗯,你有:

r.nextInt(11)

从0到10得到随机数,但你只有0-9的声音。因此,当您碰巧遇到10时,您会得到NullPointerException