我对此感到非常愚蠢,观看了多个教程和Stackoverflow问题,但仍然无法修复它。
我正在编写一个非常简单的Android mp3流式监听器。我想使用服务,因此Stream可以在后台运行。但我无法启动服务:
12-22 08:42:58.738:W / dalvikvm(1701):threadid = 1:线程退出未捕获的异常(group = 0xb3aa2ba8)
12-22 08:42:58.788:E / AndroidRuntime(1701):致命异乎寻常:主要 12-22 08:42:58.788:E / AndroidRuntime(1701):处理:com.wankoradio,PID:1701 12-22 08:42:58.788:E / AndroidRuntime(1701):java.lang.RuntimeException:无法实例化服务com.wankoradio.MediaPlayerService:java.lang.NullPointerException 12-22 08:42:58.788:E / AndroidRuntime(1701):在android.app.ActivityThread.handleCreateService(ActivityThread.java:2556) 12-22 08:42:58.788:E / AndroidRuntime(1701):在android.app.ActivityThread.access $ 1800(ActivityThread.java:135) 12-22 08:42:58.788:E / AndroidRuntime(1701):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278) 12-22 08:42:58.788:E / AndroidRuntime(1701):在android.os.Handler.dispatchMessage(Handler.java:102) 12-22 08:42:58.788:E / AndroidRuntime(1701):在android.os.Looper.loop(Looper.java:136) 12-22 08:42:58.788:E / AndroidRuntime(1701):在android.app.ActivityThread.main(ActivityThread.java:5017) 12-22 08:42:58.788:E / AndroidRuntime(1701):at java.lang.reflect.Method.invokeNative(Native Method) 12-22 08:42:58.788:E / AndroidRuntime(1701):at java.lang.reflect.Method.invoke(Method.java:515) 12-22 08:42:58.788:E / AndroidRuntime(1701):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 12-22 08:42:58.788:E / AndroidRuntime(1701):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 12-22 08:42:58.788:E / AndroidRuntime(1701):at dalvik.system.NativeStart.main(Native Method) 12-22 08:42:58.788:E / AndroidRuntime(1701):引起:java.lang.NullPointerException 12-22 08:42:58.788:E / AndroidRuntime(1701):在android.content.ContextWrapper.getSystemService(ContextWrapper.java:540) 12-22 08:42:58.788:E / AndroidRuntime(1701):at com.wankoradio.MediaPlayerService。(MediaPlayerService.java:27)
等
我的MainActivity正在启动服务,看起来有点像这样:
public class MainActivity extends Activity implements OnClickListener {
private Button pausePlayButton;
private static String ACTION_PLAY = "com.action.PLAY";
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pausePlayButton = (Button) findViewById(R.id.pausePlay_Button);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
pausePlayButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (!isPlaying) {
pausePlayButton.setBackgroundResource(R.drawable.button_pause);
Intent mServiceIntent = new Intent(getApplicationContext(), MediaPlayerService.class);
mServiceIntent.setAction(ACTION_PLAY);
startService(mServiceIntent);
我的服务看起来像这样:
public class MediaPlayerService extends Service implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener {
//Creating a matching Intent
private static final String ACTION_PLAY = "com.action.PLAY";
//Creating our main MediaPlayer
MediaPlayer mMediaPlayer = null;
//NotificationID
private static final int NOTIFICATION_ID = 1234;
//Wifi should not be shut down while our App is running, therefore we Lock it
WifiLock wifiLock = ((WifiManager) getSystemService(Context.WIFI_SERVICE))
.createWifiLock(WifiManager.WIFI_MODE_FULL, "mylock");
@Override
public void onCreate() {
super.onCreate();
Notification notification = StartNotification();
startForeground(NOTIFICATION_ID, notification);
}
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals(ACTION_PLAY)) {
//Wifi should not be shut down while our App is running, therefore we Lock it
wifiLock.acquire();
StartMediaPlayer();
}
return 0;
}
private Notification StartNotification() {
String songName = "Woop";
// assign the song name to songName
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(getApplicationContext(), MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification();
//notification.tickerText = text;
//notification.icon = R.drawable.play0;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
return notification;
}
我尝试调试它,但我的服务的onCreate或onStartCommand不会被调用
这是我的清单:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.wankoradio.MainActivity"
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:enabled="true"
android:name=".MediaPlayerService"
android:label="MediaPlayer Service" >
<intent-filter>
<action
android:name = "com.action.PLAY">
</action>
</intent-filter>
</service>
<receiver
android:name=".MainActivity">
<intent-filter>
<action
android:name = "com.action.PLAY">
</action>
</intent-filter>
</receiver>
</application>
如果我浪费你的时间,我很抱歉,这是我的第一个Android应用程序,在WinPhone上,这当然更容易。
答案 0 :(得分:1)
WifiLock wifiLock = ((WifiManager) getSystemService(Context.WIFI_SERVICE))
.createWifiLock(WifiManager.WIFI_MODE_FULL, "mylock");
系统服务在对象实例化时不可用。推迟将wifiLock
初始化为onCreate()
或更高版本。