2016-07-05 54 views
2

我在我的服務中有一個廣播接收器,當我從我的活動向它發送廣播以停止服務時,例如,我收到以下錯誤:Android BroadcastReceiver服務在向它發送廣播時給出空指針異常

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.BroadcastReceiver.onReceive(android.content.Context, android.content.Intent)' on a null object reference 

這裏是我服務類

public class PhraseService extends Service{ 

public static List<Phrase> phrases; 
private Context context; 

private static final String PHRASE_SERVICE_BROADCAST_ID = "com.cryogenos.safephrase.PHRASE_SERVICE_BROADCAST_ID"; 
private BroadcastReceiver command_broadcast_receiver; 


@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    this.context = getApplicationContext(); 
    LocalBroadcastManager.getInstance(this).registerReceiver(command_broadcast_receiver, new IntentFilter(PHRASE_SERVICE_BROADCAST_ID)); 

    Log.i("COS", "STARTED SELF"); 

    /* Handle any command sent by the app here */ 
    command_broadcast_receiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Log.i("COS", "CALLED?"); 
      try { 
       String command = intent.getStringExtra("command"); 

       /* Based on the command given, perform an action */ 
       switch (command) { 
        case "RESTART RECOGNIZER": 
         break; 
        case "STOP": 
         Toast.makeText(context, "Phrase Listener Stopped", Toast.LENGTH_SHORT).show(); 
         stopSelf(); 
         break; 
       } 
      } 
      catch (Exception e){} 
     } 
    }; 
    Intent notif_intent = new Intent(this, Home.class); 
    PendingIntent pi = PendingIntent.getActivity(this, 0, notif_intent, 0); 
    Notification notif = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle("Phrase Listener") 
      .setContentText("Listening for Phrases") 
      .setContentIntent(pi) 
      .build(); 

    startForeground(34994, notif); 
    return super.onStartCommand(intent, flags, startId); 
} 

@Override 
public void onDestroy() { 
    LocalBroadcastManager.getInstance(this).unregisterReceiver(command_broadcast_receiver); 
    super.onDestroy(); 
} 


@Override 
public IBinder onBind(Intent intent) {return null;} 
} 

這裏是活動從那裏我是從發送廣播:

public class SettingsModel { 

private Context context; 
private static final String PHRASE_SERVICE_BROADCAST_ID = "com.cryogenos.safephrase.PHRASE_SERVICE_BROADCAST_ID"; 
private LocalBroadcastManager local_broadcast_manager; 

public SettingsModel(Context context) { 
    this.context = context; 
    local_broadcast_manager = LocalBroadcastManager.getInstance(context); 
} 

public void handleSwitch(SettingsSwitch settings_switch, boolean status){ 
    switch (settings_switch){ 
     case POWER: 
      if(status) { 
       Intent i = new Intent(context, PhraseService.class); 
       context.startService(i); 
      } 
      else{ 
       Intent i = new Intent(PHRASE_SERVICE_BROADCAST_ID); 
       i.putExtra("command", "STOP"); 
       local_broadcast_manager.sendBroadcast(i); 
      } 
      break; 
     case VIBRATE: 
      break; 
     case RING: 
      break; 
     case TIMER: 
      break; 
    } 
} 
} 

我的服務是假設要持續運行,只有當我告訴它時,它應該停止。這就是爲什麼我將它設置爲前臺服務。這裏的問題是,當我試圖播放它時,我總是收到錯誤。

回答

6

在初始化之前,您正在註冊您的brodcastreceiver。因此,嘗試初始化後移動登記如下:

/* Handle any command sent by the app here */ 
    command_broadcast_receiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Log.i("COS", "CALLED?"); 
      try { 
       String command = intent.getStringExtra("command"); 

       /* Based on the command given, perform an action */ 
       switch (command) { 
        case "RESTART RECOGNIZER": 
         break; 
        case "STOP": 
         Toast.makeText(context, "Phrase Listener Stopped", Toast.LENGTH_SHORT).show(); 
         stopSelf(); 
         break; 
       } 
      } 
      catch (Exception e){} 
     } 
    }; 
LocalBroadcastManager.getInstance(this).registerReceiver(command_broadcast_receiver, new IntentFilter(PHRASE_SERVICE_BROADCAST_ID)); 

此外,您SettingsModel並不是一個活動,因爲它不會擴展Activity