Android / Java - 从另一个文件调用RingtoneManager实例方法

时间:2015-12-15 20:16:34

标签: java android-layout

希望是一个基本的(学校项目)问题:

在下面的代码中,我试图在Alerts.java中定义一个实例方法,以便在SensorService.java中满足条件时生成铃声。

为了进行调试,我添加了Log.d和一个“返回字符串” - 两者都可以从Alerts.java中运行。

对于我的生活,我无法从Alerts.java获取铃声代码。如果我将铃声代码放在SensorService.java-没有问题。但是,我想实现至少3或4种不同类型的警报(并且希望在Alerts.java中包含此代码 - 保持SensorService.java可读/可管理)。

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控制台:

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)

提前致谢!

1 个答案:

答案 0 :(得分:1)

我曾多次遇到过这种问题,我不知道为什么会这样,但我有一个可能适用于你的情况的解决方法。尝试在al.audible()调用上返回一个Ringtone对象,然后从SensorService播放它:

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

如果这不能解决您的问题,可能您应该查看铃声路径。

希望这可以帮到你。