Mediaplayer NullPointerException问题

时间:2013-02-02 23:09:22

标签: android android-mediaplayer

我在我的应用程序中使用mediaplayer。我的应用程序正常工作,但我在google play开发者控制台上看到错误报告(对于某些用户)问题出在哪里,谢谢

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    soundname= new String[]  {"Sound1","Sound2","Sound3","Sound4","Sound5","Sound6"}; 
    soundfile= new int[] {R.raw.sound1,R.raw.sound2,R.raw.sound3,R.raw.sound4,R.raw.sound5,R.raw.sound6};

    this.setContentView(R.layout.single_list_item_view);

    TextView txtProduct = (TextView) findViewById(R.id.product_label);
    Intent intent = getIntent();
    int position = intent.getExtras().getInt("position");

    txtProduct.setText(soundname[position]);
    mediaPlayer = MediaPlayer.create(this, soundfile[position]); 

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

        public void onClick(View arg0) { 
                mediaPlayer.start();         
        }
        });

错误日志; (mediaPlayer.start(); - > SingleListItem.java:60)

java.lang.NullPointerException
at com.zilsesleri.SingleListItem$1.onClick(SingleListItem.java:60)  
at android.view.View.performClick(View.java:2538)
at android.view.View$PerformClick.run(View.java:9152)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3693)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

错误在这一行mediaPlayer.start();中,你的mediaPlayer对象在这里为空,有一个空检查,并在什么情况下弄清楚它应该为null。