Android媒体播放器空指针异常

时间:2012-09-02 22:07:32

标签: android nullpointerexception android-mediaplayer

我想用mediaplayer播放声音文件。我正在播放音频文件时收到错误消息。 (“不幸的是,进程horizo​​ntalviewpaging已经停止了”)请帮助我,谢谢。

代码是;

package ro.ovidiuconeac.horizontalviewswiping;
import ro.ovidiuconeac.horizontalviewswiping.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class CustomPageChangeListener extends Activity implements OnPageChangeListener,     OnClickListener {

MediaPlayer soundplay;
private Button buttonBlue   =   null;
private Button buttonYellow =   null;
private Button buttonRed    =   null;
private Activity context    =   null;

/**
 * Constructor
 */ 

public CustomPageChangeListener(Activity context) {
this.context = context;

}

public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub

}

public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

}

public void onPageSelected(int position) {

switch (position) {
    case 0 : {
        buttonBlue = (Button)    context.findViewById(R.id.buttonBlue);
        buttonBlue.setOnClickListener(this);

        break;
    }
    case 1 : {
        buttonYellow = (Button) context.findViewById(R.id.buttonYellow);
        buttonYellow.setOnClickListener(this);
        break;
    }
    case 2 : {
        buttonRed = (Button) context.findViewById(R.id.buttonRed);
        buttonRed.setOnClickListener(this);

        break;
    }
    }   
}

public void onClick(View v) {

// Click button blue
if (buttonBlue != null && v.getId() == buttonBlue.getId()) {
    Toast.makeText(context, "Blue screen", Toast.LENGTH_SHORT).show();

}

// Click button yellow
if (buttonYellow != null && v.getId() == buttonYellow.getId()) {

    //  Toast.makeText(context, "Yellow screen",   Toast.LENGTH_SHORT).show();
    playMusic();
}

// Click button red
if (buttonRed != null && v.getId() == buttonRed.getId()) {
    Toast.makeText(context, "Red screen", Toast.LENGTH_SHORT).show();

}   
}

public void playMusic () {
soundplay = new MediaPlayer();
    try {   
    soundplay = MediaPlayer.create(getApplicationContext(), R.raw.bangyeni);
    soundplay.setVolume(1, 1);
    soundplay.start();

}
catch (Exception e){
Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show();
}   
}   

logcat的;

> 09-01 08:38:04.964: E/AndroidRuntime(631): FATAL EXCEPTION: main
> 09-01 08:38:04.964: E/AndroidRuntime(631): java.lang.NullPointerException
> 09-01 08:38:04.964: E/AndroidRuntime(631): at     android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
> 09-01 08:38:04.964: E/AndroidRuntime(631): at     ro.ovidiuconeac.horizontalviewswiping.CustomPageChangeListener.onClick(CustomPageChangeList    ener.java:80)
> 09-01 08:38:04.964: E/AndroidRuntime(631): at         android.view.View.performClick(View.java:4084)
> 09-01 08:38:04.964: E/AndroidRuntime(631): at     android.view.View$PerformClick.run(View.java:16966)
> 09-01 08:38:04.964: E/AndroidRuntime(631): at     android.os.Handler.handleCallback(Handler.java:615)
> 09-01 08:38:04.964: E/AndroidRuntime(631): at     android.os.Handler.dispatchMessage(Handler.java:92)
> 09-01 08:38:04.964: E/AndroidRuntime(631): at android.os.Looper.loop(Looper.java:137)
> 09-01 08:38:04.964: E/AndroidRuntime(631): at     android.app.ActivityThread.main(ActivityThread.java:4745)
> 09-01 08:38:04.964: E/AndroidRuntime(631): at     java.lang.reflect.Method.invokeNative(Native Method)
> 09-01 08:38:04.964: E/AndroidRuntime(631): at     java.lang.reflect.Method.invoke(Method.java:511)
> 09-01 08:38:04.964: E/AndroidRuntime(631): at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
> 09-01 08:38:04.964: E/AndroidRuntime(631): at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
> 09-01 08:38:04.964: E/AndroidRuntime(631): at dalvik.system.NativeStart.main(Native     Method)

2 个答案:

答案 0 :(得分:0)

MediaPlayer.create(getApplicationContext()

有Nullpointer

如果忘记了哪里,但在某些类Android文档是错误的!它是在询问背景,但是我给的不好。

试试这个:

MediaPlayer.create(this

答案 1 :(得分:0)

首先松开线条soundplay = new MediaPlayer();并按如下方式更改playMusic。 我假设你在资源的原始文件夹中有bangyeni。

public void playMusic () {
//soundplay = new MediaPlayer();  <--- not needed
    try {   
    soundplay = MediaPlayer.create(this, R.raw.bangyeni);
    soundplay.setVolume(1, 1);
    soundplay.start();

}