我正在尝试在电池完全充电时播放铃声。为了实现这一点,我提供了一项服务。当我第一次执行我的代码时,它第二次没用,我一直在我的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”,警报)后的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
答案 0 :(得分:3)
我怀疑这是问题所在:
String alarms = getAlarm.getString("ringtones", null);
Uri uri = Uri.parse(alarms);
看起来alarms
仍然是空的 - 所以可能在共享首选项中没有“铃声”字符串。
首先,您应该验证是这种情况(在尝试解析之前记录alarms
),然后找出为什么没有预期的偏好。