每次我在手机上运行应用程序崩溃

时间:2012-04-21 14:43:52

标签: android media-player

我想制作一个通用音板 但它不会运行,这是奇怪的,而我没有任何错误 我在网上搜索了一个解决方案,但我找不到它 这是我使用的代码:

package com.soundboard;

import java.io.IOException;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;


public class Soundboard extends Activity implements OnClickListener {
MediaPlayer player = new MediaPlayer();
Resources res = getResources();
int[] buttonIds = {R.id.sound1,R.id.sound2,R.id.sound3,R.id.sound4,R.id.sound5,R.id.sound6,R.id.sound7,R.id.sound8,R.id.sound9,R.id.sound10,R.id.sound11,R.id.sound12,R.id.sound13,R.id.sound14,R.id.sound15,R.id.sound16};
int[] soundIds = {R.raw.sound1, R.raw.sound2, R.raw.sound3, R.raw.sound4, R.raw.sound5, R.raw.sound6, R.raw.sound7, R.raw.sound8, R.raw.sound9, R.raw.sound10, R.raw.sound11, R.raw.sound12, R.raw.sound13, R.raw.sound14, R.raw.sound15, R.raw.sound16};

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

public void onClick(View v) {
    //find the index that matches the button's ID, and then reset
    //the MediaPlayer instance, set the data source to the corresponding
    //sound effect, prepare it, and start it playing.
    for(int i = 0; i < buttonIds.length; i++) {
        if(v.getId() == buttonIds[i]) {
            AssetFileDescriptor afd = getResources().openRawResourceFd(soundIds[i]);
                try {
                    player.reset();
                    player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
                    player.prepare();
                    player.start();
                    afd.close();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
} 

我希望你们能帮助我
谢谢,
流量

1 个答案:

答案 0 :(得分:0)

我认为问题是你没有在你点击的视图上设置onclickListener。 我认为视图是一个按钮。在javacode中的按钮上调用setOnclickListener()。