Getting error while playing ringtones uri
<p>I am trying to play a ringtone when battery is completely charged. To acheive this I made a service. When I execute my code for the first time it worked then for the second time it didnt and I keep getting these errors in my logcat.</p>

<pre><code>05-13 10:03:13.925: D/dalvikvm(26079): GC_EXTERNAL_ALLOC freed 25K, 49% free   2788K/5379K, external 518K/518K, paused 29ms
05-13 10:03:13.985: D/CLIPBOARD(26079): Hide Clipboard dialog at Starting input: finished by someone else... !
05-13 10:03:16.210: D/AndroidRuntime(26079): Shutting down VM
05-13 10:03:16.210: W/dalvikvm(26079): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-13 10:03:16.215: E/AndroidRuntime(26079): FATAL EXCEPTION: main
05-13 10:03:16.215: E/AndroidRuntime(26079): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000000 (has extras) } in com.zafar.batterynotify.BatteryService$1@4053f718
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.os.Handler.handleCallback(Handler.java:587)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.os.Looper.loop(Looper.java:130)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.app.ActivityThread.main(ActivityThread.java:3691)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at java.lang.reflect.Method.invokeNative(Native Method)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at java.lang.reflect.Method.invoke(Method.java:507)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at dalvik.system.NativeStart.main(Native Method)
05-13 10:03:16.215: E/AndroidRuntime(26079): Caused by: java.lang.NullPointerException: uriString
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.net.Uri$StringUri.&lt;init&gt;(Uri.java:420)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.net.Uri$StringUri.&lt;init&gt;(Uri.java:410)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.net.Uri.parse(Uri.java:382)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at com.zafar.batterynotify.BatteryService$1.onReceive(BatteryService.java:49)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
05-13 10:03:16.215: E/AndroidRuntime(26079):    ... 9 more
</code></pre>

<p>According to logcat error is somewhere on <code>Uri uri = Uri.parse(alarms);</code>.</p>

<p>Here is my code for the service class</p>

<pre><code>public class BatteryService extends Service {
Notify notification = new Notify();
BatteryAlarm alarm = new BatteryAlarm();
private MediaPlayer mMediaPlayer;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    notification.initNotification(this);
    this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    return START_STICKY;
}

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context c, Intent i) {

        int level = i.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int plugged = i.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);

        SharedPreferences getAlarm = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        String alarms = getAlarm.getString("ringtones", null);
        Uri uri = Uri.parse(alarms);

        if(plugged == 2) {

            if(level &lt; 100) {

                if(uri != null) {
                    playAlarm(c, uri);
                }
                //CharSequence t = (CharSequence) uri;
                //Toast.makeText(c, "Set", Toast.LENGTH_LONG).show();

            }

        } else if (plugged == 0) {
            if(uri != null) {
                stopAlarm();
            }
            Toast.makeText(c, "Unplugged", Toast.LENGTH_LONG).show();

        }
    }
};

private void playAlarm(Context c, Uri uri) {
    //Uri uri = Uri.parse(uri);
    mMediaPlayer = new MediaPlayer();
    try {
        mMediaPlayer.reset();
        mMediaPlayer.setDataSource(getBaseContext(), uri);
        final AudioManager audioManager = (AudioManager) c.getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        onDestroy();
    }
}

private void stopAlarm() {
    mMediaPlayer.stop();
    onDestroy();
}

public void onDestroy() {
    super.onDestroy();
    notification.cancelNotification(this);
    unregisterReceiver(this.mBatInfoReceiver);
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
}

}
</code></pre>

<p>Help me please. Where am I making mistake due to which I am keep getting these errors</p>

<p><strong>Logcat after adding Log.d("TAG", alarms)</strong></p>

<pre><code>05-13 10:29:43.450: D/AndroidRuntime(28062): Shutting down VM
05-13 10:29:43.450: W/dalvikvm(28062): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-13 10:29:43.455: E/AndroidRuntime(28062): FATAL EXCEPTION: main
05-13 10:29:43.455: E/AndroidRuntime(28062): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000000 (has extras) } in com.zafar.batterynotify.BatteryService$1@40547e78
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.os.Handler.handleCallback(Handler.java:587)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.os.Looper.loop(Looper.java:130)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.app.ActivityThread.main(ActivityThread.java:3691)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at java.lang.reflect.Method.invokeNative(Native Method)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at java.lang.reflect.Method.invoke(Method.java:507)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at dalvik.system.NativeStart.main(Native Method)
05-13 10:29:43.455: E/AndroidRuntime(28062): Caused by: java.lang.NullPointerException: println needs a message
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.util.Log.println_native(Native Method)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.util.Log.d(Log.java:137)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at com.zafar.batterynotify.BatteryService$1.onReceive(BatteryService.java:51)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
05-13 10:29:43.455: E/AndroidRuntime(28062):    ... 9 more
</code></pre>
