Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android应用程序在手机启动时崩溃_Android_Service_Sharedpreferences - Fatal编程技术网

Android应用程序在手机启动时崩溃

Android应用程序在手机启动时崩溃,android,service,sharedpreferences,Android,Service,Sharedpreferences,我有权接收已完成的引导,并已指定要运行的接收器 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <receiver android:name="xyz.android.MyAlarm" > <intent-filter> <action android:name="android.intent.action.

我有权接收已完成的引导,并已指定要运行的接收器

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<receiver android:name="xyz.android.MyAlarm" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
下面是我的邀请服务代码的一段

public class InviteService extends IntentService {

private String userId;
private SharedPreferences settingsPrefs;

public InviteService() {
    super("InviteService");
}

@Override
protected void onHandleIntent(Intent intent) {

    if (Utils.isOnline(this)) {
        userId = getSharedPreferences("user-session", MODE_PRIVATE)
                .getString("userId", null);
        settingsPrefs = getSharedPreferences(userId, MODE_PRIVATE);

        Log.d("turnOffAlarm",
                "" + settingsPrefs.getBoolean("turnOffAlarm", false));
        if (userId == null
                || settingsPrefs.getBoolean("turnOffAlarm", false)) {
            stopService(intent);
            return;
        }
      }
    }
当用户重新启动手机时,会出现以下错误消息

java.lang.NullPointerException
at xyz.android.InviteService.onHandleIntent(InviteService.java:134)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.os.HandlerThread.run(HandlerThread.java:60)
我得到空指针异常的那一行是记录关闭警报的那一行。这就是发生的事情。出于某种原因,userId获取的值为null,而它本不应该为null。此值已存储在首选项文件中,在手机重新启动之前不会出现问题


我无法在emulator上重现此错误,有什么建议吗?

getString(“userd”,null)-这是问题中的错误还是您的代码(缺少I)?是的,这是一个错误,让我修复。
java.lang.NullPointerException
at xyz.android.InviteService.onHandleIntent(InviteService.java:134)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.os.HandlerThread.run(HandlerThread.java:60)