1

大家好,我只是用来启动 android,我在媒体播放器中遇到问题...我的代码在下面...

public class UrlClass extends Activity {

private MediaPlayer mPlayer;
private Button playbtn;
private Button stopbtn;


private String urlpath = "Here is URL";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.url);


    playbtn = (Button)findViewById(R.id.play);
    stopbtn = (Button) findViewById(R.id.stop);

    playbtn.setOnClickListener(new OnClickListener() {

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



                mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                try {
        mPlayer.prepareAsync();


            mPlayer.start();

        }
    });

    stopbtn.setOnClickListener(new OnClickListener() {

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

            if(mPlayer!=null && mPlayer.isPlaying())
            {
                mPlayer.stop();
            }

        }
    });

}

public String getUrlpath() {
    return urlpath;
}

public void setUrlpath(String urlpath) {
    this.urlpath = "http://android.programmerguru.com/wp-content/uploads/2013/04/hosannatelugu.mp3";
}   
}

为什么会出现这个错误?

09-26 05:04:53.249: E/AndroidRuntime(1950): FATAL EXCEPTION: main
09-26 05:04:53.249: E/AndroidRuntime(1950): java.lang.NullPointerException
09-26 05:04:53.249: E/AndroidRuntime(1950):     at com.androidhive.musicplayer.UrlClass$1.onClick(UrlClass.java:37)
09-26 05:04:53.249: E/AndroidRuntime(1950):     at android.view.View.performClick(View.java:4240)
09-26 05:04:53.249: E/AndroidRuntime(1950):     at android.view.View$PerformClick.run(View.java:17721)
09-26 05:04:53.249: E/AndroidRuntime(1950):     at android.os.Handler.handleCallback(Handler.java:730)
09-26 05:04:53.249: E/AndroidRuntime(1950):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-26 05:04:53.249: E/AndroidRuntime(1950):     at android.os.Looper.loop(Looper.java:137)
09-26 05:04:53.249: E/AndroidRuntime(1950):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-26 05:04:53.249: E/AndroidRuntime(1950):     at java.lang.reflect.Method.invokeNative(Native Method)
09-26 05:04:53.249: E/AndroidRuntime(1950):     at java.lang.reflect.Method.invoke(Method.java:525)
09-26 05:04:53.249: E/AndroidRuntime(1950):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-26 05:04:53.249: E/AndroidRuntime(1950):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-26 05:04:53.249: E/AndroidRuntime(1950):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

You have not initialzed mPlayer. Initialize mPlayer

mPlayer = MeadiaPlayer.create(ActivityName.this, your uri);

Check the link for more info on MediaPlayer

http://developer.android.com/reference/android/media/MediaPlayer.html

于 2013-09-26T09:32:17.530 回答