简体   繁体   中英

Android: Unable to instantiate service Java.NullPointerException

I feel very stupid about this, watched multiple Tutorials and Stackoverflow questions, but still can't fix it.

I'm writing a very simple Android mp3 Stream listener. I want to use a Sevice so the Stream can be running in the Background. But i can't start a Service:

12-22 08:42:58.738: W/dalvikvm(1701): threadid=1: thread exiting with uncaught exception (group=0xb3aa2ba8)

12-22 08:42:58.788: E/AndroidRuntime(1701): FATAL EXCEPTION: main 12-22 08:42:58.788: E/AndroidRuntime(1701): Process: com.wankoradio, PID: 1701 12-22 08:42:58.788: E/AndroidRuntime(1701): java.lang.RuntimeException: Unable to instantiate service com.wankoradio.MediaPlayerService: java.lang.NullPointerException 12-22 08:42:58.788: E/AndroidRuntime(1701): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2556) 12-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 j ava.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): Caused by: 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): at com.wankoradio.MediaPlayerService.(MediaPlayerService.java:27)

etc

My MainActivity is starting the Service and looks kinda like this:

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);

and my Service looks like this:

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;
    }

I tried to debug it but onCreate or onStartCommand of my Service won't get called

and here is my Manifest:

<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>

I'm sorry if I'm wasting your time, this is my first Android application, on WinPhone this is certainly easier to do.

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

System services are not available at object instantiation time. Postpone initializing your wifiLock to onCreate() or later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM