2

Je souhaite démarrer mon service via le gestionnaire d'alarmes. J'ai écrit ci-dessous le code. Ce code fonctionne bien quand mon application est en arrière-plan ou au premier plan mais quand je tue mon application (je veux dire glisser l'application des applications récentes) le programme obtient une erreur. Je ne sais pas quel est mon problème et comment y remédier.nullPointerException pour définir le gestionnaire d'alarmes dans une application désactivée

Erreur:

Caused by: java.lang.NullPointerException at com.company.test.helper.ServiceSchedular.startService(ServiceSchedular.java:74) 

J'ai une classe qui va de l'application comme ci-dessous:

public class KITILApplication extends Application{ 

private static Context context; 
@Override 
public void onCreate() { 
    // TODO Auto-generated method stub 
    super.onCreate(); 
    ACRA.init(this); 
    ORMDroidApplication.initialize(this); 
    KITILApplication.context=getApplicationContext(); 
} 

public static Context getappContext(){ 
    return KITILApplication.context; 
} 
} 

et mon ServiceSchedularService pour les alarmes de planification pour le service de départ est en dessous:

public class ServiceSchedular { 
    //Called from my main activity 
    private ServiceSchedular() { 
     alarmManager = (AlarmManager) KITILApplication.getappContext().getSystemService(Context.ALARM_SERVICE); 
    } 
    private static void startService(Date startDate) { 
    uniqueIdForStart = (int) Calendar.getInstance().getTimeInMillis(); 
    //PlEASE NOTICE: I checked my application via both of startintent(comment startIntent and startIntent) 
    Intent startIntent = new Intent(KITILApplication.getappContext(), LocationService.class); 
    //Intent startIntent = new Intent("com.company.test.START_SERVICE"); 
    PendingIntent startPendingIntent = PendingIntent.getService(KITILApplication.getappContext(), uniqueIdForStart, startIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    //I get error from below line !! Line = 74 
    alarmManager.set(AlarmManager.RTC_WAKEUP, millisToStartDate, startPendingIntent); 
    stopService(startDate); 
} 

public static void checkNextPeriod() { 
     . 
     . 
     startService(new Date(millisDate)); 
} 
} 

J'ai un broadcastReciever appelé checkNextPeriod et dans checkNextPeriod, je appelée méthode startService.

AndroidManifest.xml:

<service android:name=".Service.LocationService"> 
     <intent-filter> 
      <action android:name="com.company.test.START_SERVICE"></action> 
     </intent-filter> 
    </service> 

Je ne sais pas mon problème est pour objet AlarmManager ou contexte de l'objet ou de l'application intention ou d'une autre chose. Aidez-moi, s'il vous plaît.

+0

avez-vous ajouté ** android: name = ". KITILApplication ** dans le fichier manifeste –

+0

Merci d'avoir répondu cette partie dans AndroidManifest – Fahim

+0

Au lieu de KITILApplication. context = getApplicationContext(); essayez ceci: context = this; – nomag

Répondre

0

Votre problème est ici:

private ServiceSchedular() { 
    alarmManager = (AlarmManager) KITILApplication.getappContext().getSystemService(Context.ALARM_SERVICE); 
} 

Ne pas le faire dans un constructeur. Appelez simplement getSystemService() lorsque vous en avez besoin.