2

Hopefully, a basic (school project) question:

In the code that follows, I am trying to define an instance method in Alerts.java in order to generate a ringtone when a condition is met in SensorService.java.

For debugging, I added Log.d and a 'return string'- both work wonderfully from Alerts.java.

For the life of me, I cannot get the ringtone code to work from Alerts.java. If I place the ringtone code in SensorService.java- no issues. However, I want to implement at least 3 or 4 different types of alerts (and would prefer to contain this code in Alerts.java-- keeping SensorService.java readable/manageable).

filename: SensorService.java

[...]
public class SensorService extends Service implements SensorEventListener {
[...]
public void onSensorChanged(SensorEvent event) {
    float x = event.values[0];
    Log.d("ADebugTag", Float.toString(x));

    Alerts al = new Alerts ();
    String myInstanceString = al.audible();
    System.out.println(myInstanceString);
    // Generate an audible tone when Sensor detects movement.
    if ( x > 2) al.audible();
}
[...]

filename: Alerts.java

[...]
public class Alerts {
[...]
public String audible () {
    Log.d("1", "tick!");
    Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    Ringtone ringtoneSound = RingtoneManager.getRingtone(context, ringtoneUri);
    ringtoneSound.play();
    return "This is an instance function.";
}
[...]

DEBUG Console:

12-15 14:29:49.994  12899-12899/com.pikpocket.pikpocket E/RingtoneManager﹕
Failed to open ringtone content://settings/system/alarm_alert:
java.lang.NullPointerException
12-15 14:29:49.994  12899-12899/com.pikpocket.pikpocket W/dalvikvm﹕ 
threadid=1: thread exiting with uncaught exception (group=0x41569438)
12-15 14:29:49.994  12899-12899/com.pikpocket.pikpocket E/AndroidRuntime﹕
FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.pikpocket.pikpocket.Alerts.audible(Alerts.java:31)
        at 
com.pikpocket.pikpocket.SensorService.onSensorChanged(SensorService.java:78)
        at 
android.hardware.SystemSensorManager$ListenerDelegate$1.handleMessage(SystemSensorManager.java:250)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4918)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
        at dalvik.system.NativeStart.main(Native Method)

Thanks in advance!

XMAN
  • 176
  • 1
  • 3
  • 12
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Petter Friberg Dec 15 '15 at 20:19
  • 1
    java.lang.NullPointerException, means thet your not able to create the object probably ringtoneSound (check line), context == null?. Checkout http://www.programcreek.com/java-api-examples/android.media.RingtoneManager – Petter Friberg Dec 15 '15 at 20:21
  • @Petter: Thanks for the INFO. Cannot track down where null is. However looking over the good examples you've provided (although still a little tricky for me to wrap my brain around). – XMAN Dec 15 '15 at 21:23

1 Answers1

1

I've had this kind of problems several times, I don't know the reason why this happens, but I have a workaround that probably works for your case. Try returning a Ringtone object on al.audible() call and then play it from SensorService:

in SensorService.java

public class SensorService extends Service implements SensorEventListener {
public void onSensorChanged(SensorEvent event) {
    float x = event.values[0];
    Log.d("ADebugTag", Float.toString(x));

    Alerts al = new Alerts ();
    Ringtone myRingtone = al.audible();
    myRingtone.play();
}}

in Alerts.java

public class Alerts {
public Ringtone audible () {
    Log.d("1", "tick!");
    Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    Ringtone ringtoneSound = RingtoneManager.getRingtone(context, ringtoneUri);
    return ringtoneSound;
}}

If this doesn't solve your problem, probably you should have a look to the ringtone path.

Hope this can help you.

Gerard Reches
  • 3,048
  • 3
  • 28
  • 39
  • 1
    Thanks Gerard: Following error ensues"12251-12251/com.pikpocket.pikpocket E/RingtoneManager﹕ Failed to open ringtone content://settings/system/alarm_alert: java.lang.NullPointerException". Odd, if I put the ringtone code in the SensorService file, I do not encounter any issue with 'ringtone path'. I'll keep digging. – XMAN Dec 15 '15 at 20:59
  • 1
    @XO. I've had problems with NullPointerException several times when I tried to execute system functions through another file instance, if the ringtone works on your SensorService.java but not in the Alerts.java, I'm pretty sure you have the kind of problem I'm trying to say. Can you try to initialize your desired Ringtone on the Alerts constructor and then get it from the SensorService using a GetMyRingtone() getter? – Gerard Reches Dec 15 '15 at 21:12
  • I'm understanding your logic, but the code is not coming to mind (just picked up JAVA a few weeks ago. Any snippet I can snatch from the following [link](http://www.programcreek.com/java-api-examples/android.media.RingtoneManager) – XMAN Dec 15 '15 at 21:41