我在编写添加背景音乐的程序时,参考了这里的代码http://apps.hi.baidu.com/share/detail/18496230我的工程中有两个类,一个是music类,继承activity,另一个是musicServer类,集成service。music类如下:
package mx.music;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class music extends Activity {
    /** Called when the activity is first created. */

private Intent intent = new Intent("mx.music.MUSIC");
private Button startButton;
private Button stopButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startButton = (Button) findViewById(R.id.startButton);
        stopButton = (Button) findViewById(R.id.stopButton);
        startButton.setOnClickListener(new startListener());
        stopButton.setOnClickListener(new stopListener());
        
       
    }
    
    class startListener implements OnClickListener{ public void onClick(View v) {
// TODO Auto-generated method stub
 startService(intent);
}
    
    }
    
    class stopListener implements OnClickListener{ public void onClick(View v) {
// TODO Auto-generated method stub
stopService(intent);
}
    
    }
    
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();

}
    
    
    
   
}
musicserver类如下:
package mx.music;import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;public class musicServer extends Service{ private MediaPlayer mp;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
if(mp==null){
mp=MediaPlayer.create(this, R.raw.mmp);
mp.start();/*显示这里空指针异常,但是我在上句都已经定义了啊,并且在raw中存在mmp的MP3文件*/
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mp.stop();
}

}

解决方案 »

  1.   

    androidmanifest中的代码如下:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="mx.music"
          android:versionCode="1"
          android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".music"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity> <service android:name=".musicServer">
    <intent-filter>
    <action android:name="mx.music.MUSIC"/>
    <category android:name="android.intent.category.DEFAULT"/>

    </intent-filter>
    </service>

        </application>
    </manifest>
      

  2.   

    我把那句话添加在mp.start之前,使用try,catch语句,可是还是抱空指针异常错误,双击之后,显示错误在这里mp.prepare();可是我明显在前一行使用了create方法了,怎么还抱错呢?
      

  3.   

    单步跟进MediaPlayer.create去看看
    mp=MediaPlayer.create(this, R.raw.mmp);
      

  4.   

    没源代码应该进不去
    MediaPlayer我用的时候也在prepare那报错,不过不是空指针
    我后来这样搞定的
    FileInputStream audioFis = null;
    try {
    audioFis = new FileInputStream(filepath);

    mp.setDataSource(audioFis.getFD()); mp.prepare();

    mp.start();
      

  5.   

    public static MediaPlayer create (Context context, int resid)
    Returns
    a MediaPlayer object, or null if creation failed 
    看来是creation failed