MyService类包含静态字符串
package com.suprabhatam.alarm;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class MyService extends Service {
private static final String TAG = "MyService";
static public MediaPlayer mp;
final static public String start_pause_stop_audio = "start_pause_stop_audio";
final static public String start_audio = "start_audio";
final static public String pause_audio = "pause_audio";
final static public String stop_audio = "stop_audio";
@Override
public int onStartCommand (Intent intent, int flags, int startId) {
Calendar cal = Calendar.getInstance();
cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Log.d(TAG, sdf.format(cal.getTime()));
if (mp == null) {
mp = MediaPlayer.create(this, R.raw.venkateshwara_suprabhatam);
}
final String s_s_audio = intent.getStringExtra(MyService.start_pause_stop_audio);
Log.d(TAG, "onStartCommand: " + s_s_audio);
if (s_s_audio.equals(start_audio)) {
if (!mp.isPlaying()) {
mp.start();
} else {
Log.d(TAG, "already playing");
}
} else if (s_s_audio.equals(pause_audio)) {
mp.pause();
} else if (s_s_audio.equals(stop_audio)) {
mp.seekTo(0);
mp.pause();
} else {
Log.d(TAG, "UNKNOWN: " + s_s_audio);
}
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
崩溃应用程序,adb输出上显示以下消息。
ANRAppManager﹕ !!! It is not under singleton mode, U can't use it. !!!
只有当应用程序不在内存中时才会发生崩溃(kill)并且 MyReceiver 会尝试访问静态字符串。
在创建对象之前,静态字符串看起来无效。在C ++中,静态字符串转到数据段并在应用程序启动时有效,java中的规则是不同的吗?
更新1:添加logcat消息
01-20 21:53:32.849 2396-2396/com.suprabhatam.alarm D/jdwp﹕ sendBufferedRequest : len=0x45
01-20 21:53:32.854 2396-2396/com.suprabhatam.alarm W/asset﹕ AssetManager-->addDefaultAssets CIP path not exsit!
01-20 21:53:32.875 2396-2396/com.suprabhatam.alarm D/dalvikvm﹕ open_cached_dex_file : /data/app/com.suprabhatam.alarm-1.apk /data/dalvik-cache/data@app@com.suprabhatam.alarm-1.apk@classes.dex
01-20 21:53:32.897 2396-2396/com.suprabhatam.alarm D/MyService﹕ 21:53:32
01-20 21:53:32.959 2396-2396/com.suprabhatam.alarm D/MediaPlayer﹕ Don't notify duration to com.suprabhatam.alarm!
01-20 21:53:32.983 2396-2396/com.suprabhatam.alarm D/AndroidRuntime﹕ Shutting down VM
01-20 21:53:32.983 2396-2396/com.suprabhatam.alarm W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41bea9a8)
01-20 21:53:32.984 2396-2396/com.suprabhatam.alarm W/dalvikvm﹕ threadid=1: uncaught exception occurred
01-20 21:53:32.984 2396-2396/com.suprabhatam.alarm W/System.err﹕ java.lang.RuntimeException: Unable to start service com.suprabhatam.alarm.MyService@421b4720 with null: java.lang.NullPointerException
01-20 21:53:32.984 2396-2396/com.suprabhatam.alarm W/System.err﹕ at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2867)
01-20 21:53:32.984 2396-2396/com.suprabhatam.alarm W/System.err﹕ at android.app.ActivityThread.access$1900(ActivityThread.java:165)
01-20 21:53:32.984 2396-2396/com.suprabhatam.alarm W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
01-20 21:53:32.984 2396-2396/com.suprabhatam.alarm W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:107)
01-20 21:53:32.984 2396-2396/com.suprabhatam.alarm W/System.err﹕ at android.os.Looper.loop(Looper.java:194)
01-20 21:53:32.984 2396-2396/com.suprabhatam.alarm W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5391)
01-20 21:53:32.985 2396-2396/com.suprabhatam.alarm W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
01-20 21:53:32.985 2396-2396/com.suprabhatam.alarm W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
01-20 21:53:32.985 2396-2396/com.suprabhatam.alarm W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-20 21:53:32.985 2396-2396/com.suprabhatam.alarm W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-20 21:53:32.985 2396-2396/com.suprabhatam.alarm W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
01-20 21:53:32.985 2396-2396/com.suprabhatam.alarm W/System.err﹕ Caused by: java.lang.NullPointerException
01-20 21:53:32.986 2396-2396/com.suprabhatam.alarm W/System.err﹕ at com.suprabhatam.alarm.MyService.onStartCommand(MyService.java:34)
01-20 21:53:32.986 2396-2396/com.suprabhatam.alarm W/System.err﹕ at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2850)
01-20 21:53:32.986 2396-2396/com.suprabhatam.alarm W/System.err﹕ ... 10 more
01-20 21:53:32.986 2396-2396/com.suprabhatam.alarm W/dalvikvm﹕ threadid=1: calling UncaughtExceptionHandler
01-20 21:53:32.987 2396-2396/com.suprabhatam.alarm E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start service com.suprabhatam.alarm.MyService@421b4720 with null: java.lang.NullPointerException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2867)
at android.app.ActivityThread.access$1900(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.suprabhatam.alarm.MyService.onStartCommand(MyService.java:34)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2850)
at android.app.ActivityThread.access$1900(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
更新2秒崩溃
01-20 21:56:23.332 2793-2799/com.suprabhatam.alarm D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x400000DF, flags=0x0, dataLen=0x8
01-20 21:56:42.336 2793-2805/com.suprabhatam.alarm D/ANRAppManager﹕ !!! It is not under singleton mode, U can't use it. !!!
01-20 21:57:33.091 2793-2793/com.suprabhatam.alarm I/Process﹕ Sending signal. PID: 2793 SIG: 9
01-20 21:57:38.252 3008-3008/com.suprabhatam.alarm D/MediaPlayer﹕ Don't notify duration to com.suprabhatam.alarm!
01-20 21:57:38.260 3008-3008/com.suprabhatam.alarm D/AndroidRuntime﹕ Shutting down VM
01-20 21:57:38.260 3008-3008/com.suprabhatam.alarm W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41bea9a8)
01-20 21:57:38.260 3008-3008/com.suprabhatam.alarm W/dalvikvm﹕ threadid=1: uncaught exception occurred
01-20 21:57:38.260 3008-3008/com.suprabhatam.alarm W/System.err﹕ java.lang.RuntimeException: Unable to start service com.suprabhatam.alarm.MyService@421af568 with null: java.lang.NullPointerException
01-20 21:57:38.260 3008-3008/com.suprabhatam.alarm W/System.err﹕ at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2867)
01-20 21:57:38.260 3008-3008/com.suprabhatam.alarm W/System.err﹕ at android.app.ActivityThread.access$1900(ActivityThread.java:165)
01-20 21:57:38.260 3008-3008/com.suprabhatam.alarm W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
01-20 21:57:38.260 3008-3008/com.suprabhatam.alarm W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:107)
01-20 21:57:38.260 3008-3008/com.suprabhatam.alarm W/System.err﹕ at android.os.Looper.loop(Looper.java:194)
01-20 21:57:38.261 3008-3008/com.suprabhatam.alarm W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5391)
01-20 21:57:38.261 3008-3008/com.suprabhatam.alarm W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
01-20 21:57:38.261 3008-3008/com.suprabhatam.alarm W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
01-20 21:57:38.261 3008-3008/com.suprabhatam.alarm W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-20 21:57:38.261 3008-3008/com.suprabhatam.alarm W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-20 21:57:38.261 3008-3008/com.suprabhatam.alarm W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
01-20 21:57:38.261 3008-3008/com.suprabhatam.alarm W/System.err﹕ Caused by: java.lang.NullPointerException
01-20 21:57:38.261 3008-3008/com.suprabhatam.alarm W/System.err﹕ at com.suprabhatam.alarm.MyService.onStartCommand(MyService.java:34)
01-20 21:57:38.261 3008-3008/com.suprabhatam.alarm W/System.err﹕ at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2850)
01-20 21:57:38.261 3008-3008/com.suprabhatam.alarm W/System.err﹕ ... 10 more
01-20 21:57:38.261 3008-3008/com.suprabhatam.alarm W/dalvikvm﹕ threadid=1: calling UncaughtExceptionHandler
01-20 21:57:38.262 3008-3008/com.suprabhatam.alarm E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start service com.suprabhatam.alarm.MyService@421af568 with null: java.lang.NullPointerException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2867)
at android.app.ActivityThread.access$1900(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.suprabhatam.alarm.MyService.onStartCommand(MyService.java:34)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2850)
at android.app.ActivityThread.access$1900(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
01-20 21:57:58.221 3008-3020/com.suprabhatam.alarm D/ANRAppManager﹕ !!! It is not under singleton mode, U can't use it. !!!
01-20 22:02:38.277 3008-3008/com.suprabhatam.alarm I/Process﹕ Sending signal. PID: 3008 SIG: 9
更新了3个崩溃位置
崩溃发生在MyService.java的第32行
final String s_s_audio = intent.getStringExtra(MyService.start_pause_stop_audio)
01-20 22:22:42.230 6257-6257 / com.suprabhatam.alarm E / MyService:exception 显示java.lang.NullPointerException at com.suprabhatam.alarm.MyService.onStartCommand(MyService.java:32) 在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2850) 在android.app.ActivityThread.access $ 1900(ActivityThread.java:165)
问题似乎与intent.getStringExtra()方法
有关广播接收器发送以下事件。
Intent myIntent = new Intent(context, MyService.class);
myIntent.putExtra(MyService.start_pause_stop_audio, MyService.start_audio);
context.startService(myIntent);
如何调试此问题。
答案 0 :(得分:2)
在执行任何操作之前,您应该检查onStartCommand()
中收到的意图参数是否为空。 这是因为如果服务被系统杀死然后重新启动,Intent
可以为null,并且您不要求重新发送原始意图。
请仔细查看here并最终将onStartCommand()
的结果值更改为START_REDELIVER_INTENT
,
答案 1 :(得分:0)
更好的解决方案是从一个公共类中获取字符串,例如YourConstants.java和此类中的所有成员: public static final String;
然后当你需要它们时,无论你的项目在哪里打电话 YourConstants.NAME_OFF_YOUR_VARIABLE,你不会有这个问题。
我总是使用Constants类作为我的公共静态最终变量。