2013-12-22 54 views
1

我觉得这很愚蠢,看了多个教程和Stackoverflow的问题,但仍无法解决它。Android:无法实例化服务Java.NullPointerException

我正在写一个非常简单的Android mp3流监听器。我想使用服务,以便Stream可以在后台运行。但我不能启动一个服务:

8月12日至22日:42:58.738:W/dalvikvm(1701):线程ID = 1:螺纹与未捕获的异常(组= 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) 1 2-22 08:42:58.788:E/AndroidRuntime(1701):at android.app.ActivityThread.access $ 1800(ActivityThread.java:135) 12-22 08:42:58.788:E/AndroidRuntime(1701):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278) 12-22 08:42:58.788:E/AndroidRuntime(1701):at android.os.Handler.dispatchMessage(Handler.java:102) 12 -22 08:42:58.788:E/AndroidRuntime(1701):at android.os.Looper.loop(Looper.java:136) 12-22 08:42:58.788:E/AndroidRuntime(1701):at 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):在java.lang.reflect.Method.invoke(Method.java:515) 12-22 08:42:58.788:E/AndroidRuntime(1701):at com.android.int ernal.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):at android.content.ContextWrapper.getSystemService(ContextWrapper.java:540) 12-22 08:42:58.788:E/AndroidRuntime(1701):在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上这当然更容易做到。

回答

1
WifiLock wifiLock = ((WifiManager) getSystemService(Context.WIFI_SERVICE)) 
     .createWifiLock(WifiManager.WIFI_MODE_FULL, "mylock"); 

系统服务在对象实例化时不可用。推迟初始化您的wifiLockonCreate()或更高版本。

+0

谢谢,即时测试它atm,将改变我的问题,如果问题仍然存在 – Master117

+0

哦,男人,谢谢,确实是这个问题。我更改了通知管理器和其他一切,以便在OnCreate中更新,并忘记了WifiLock。我发现catLog很难阅读。将在9分钟内接受。 – Master117

相关问题