Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid can't bind to service
    primarykey
    data
    text
    <p>New to android, trying to figure out Services. I'm trying to bind a service to an activity, I'm following the examples in the documentation, but I keep getting a NullPointerException on the line marked below(appService.playSong(title)). Checking it in the debugger reveals that appService is indeed null. </p> <pre><code>public class Song extends Activity implements OnClickListener,Runnable { protected static int currentPosition; private ProgressBar progress; private TextView songTitle; private MPService appService; private ServiceConnection onService = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder rawBinder) { appService = ((MPService.LocalBinder)rawBinder).getService(); } public void onServiceDisconnected(ComponentName classname) { appService = null; } }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.song); Intent bindIntent = new Intent(Song.this,MPService.class); bindService(bindIntent,onService, Context.BIND_AUTO_CREATE); Bundle b = getIntent().getBundleExtra("songdata"); String title = b.getString("song title"); // ... appService.playSong(title); // nullpointerexception // ... } </code></pre> <p>Here's the relevant part of the service:</p> <pre><code>package org.example.music; // imports public class MPService extends Service { private MediaPlayer mp; public static int currentPosition = 0; public List&lt;String&gt; songs = new ArrayList&lt;String&gt;(); public static String songTitle; private static final String MEDIA_PATH = new String("/mnt/sdcard/"); @Override public void onCreate() { super.onCreate(); mp = new MediaPlayer(); songs = Music.songs; } @Override public int onStartCommand(Intent intent, int flags, int startId) { return Service.START_STICKY; } public class LocalBinder extends Binder { MPService getService() { return MPService.this; } } private final IBinder binder = new LocalBinder(); @Override public IBinder onBind(Intent intent) { return binder; } public void playSong(String songPath) { try { mp.reset(); mp.setDataSource(songPath); mp.prepare(); mp.start(); mp.setOnCompletionListener(new OnCompletionListener() { public void onCompletion(MediaPlayer arg0) { nextSong(); } }); songTitle = songPath.substring(12,songPath.length()-4); } catch (IOException e) { Log.v(getString(R.string.app_name),e.getMessage()); } } public void nextSong() { if (++currentPosition &gt;= songs.size()) { currentPosition = 0; } String song = MEDIA_PATH+songs.get(currentPosition); playSong(song); } public void prevSong() { if (--currentPosition&lt;0) { currentPosition=songs.size()-1; } String song = Music.MEDIA_PATH+songs.get(currentPosition); playSong(song); } public int getSongPosition() { return mp.getCurrentPosition(); } public MediaPlayer getMP() { return mp; } } </code></pre> <p>I have registered the service in AndroidManifest.xml and set android:enabled="true". Do you see any obvious mistakes here?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload