gpt4 book ai didi

java - 播放铃声 uri 时出现错误

转载 作者:行者123 更新时间:2023-12-02 00:23:11 24 4
gpt4 key购买 nike

我尝试在电池充满电时播放铃声。为了实现这一目标,我做了一项服务。当我第一次执行代码时,它起作用了,然后第二次就不起作用了,并且我不断在 logcat 中收到这些错误。

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.<init>(Uri.java:420)
05-13 10:03:16.215: E/AndroidRuntime(26079): at android.net.Uri$StringUri.<init>(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

根据 logcat 错误位于 Uri uri = Uri.parse(alarms); 上的某处。

这是我的服务类代码

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

}

请帮帮我。我在哪里犯了错误,导致我不断收到这些错误

添加Log.d("TAG", Alarms)后的Logcat

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

最佳答案

我怀疑这就是问题所在:

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

看起来像alarms仍然为空 - 所以大概共享首选项中没有“铃声”字符串。

首先,您应该验证情况是否如此(在尝试解析之前记录 alarms),然后找出为什么预期的偏好不存在。

关于java - 播放铃声 uri 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10569941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com