-1

나는 다음과 같은 오류가 계속 : 방송 수신기 널 포인터 안드로이드

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

내 서비스 클래스에서 방송을 수신하려고

.

서비스 :

내 주요 활동에 사용자 정의 개체 (Journ)를 보내는 브로드 캐스트를 보낼 수있는 코드이다
@Override 
public void onDestroy(){ 
    super.onDestroy(); 
    Intent intent = new Intent("UpdateLocation"); 
    intent.putExtra("Location",journ); 
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent); 
} 

.

홈페이지 :

@Override 
protected void onCreate(Bundle savedInstanceState) 

    LocalBroadcastManager.getInstance(this).registerReceiver(
      mReceiver, new IntentFilter("UpdateLocation")); 

    //Listen for service to send location data 
    mReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      temp= intent.getExtras().getParcelable("Location"); 
     } 
    }; 
} 

    @Override 
    public void onPause() { 
     if (!tracking) { 
      finish(); 
     } 
     //Run service in background to keep track of location 
    startService(new Intent(this, locationService.class)); 
     super.onPause(); 
    } 

    @Override 
    public void onResume() { 
     if (!tracking) { 
      return; 
     } 
     if (mGoogleApiClient != null) { 
      //Reconnect to google maps 
      mGoogleApiClient.reconnect(); 
     } 
     stopService(new Intent(this, locationService.class)); 
     super.onResume(); 
    } 

나는 내 앱이 배경이며이 서비스를 다시 시작할 때 때 실행 내 서비스 클래스에서 개체를 전달하려고이 일에 대해 이동하는 방법을 잘 모르겠습니다 중지하고 수집 한 데이터를 내 주요 활동으로 보내야합니다.

그러나이 방법은 효과가 없습니까?

사람들이 내 작성 방법에 더 많은 코드가 포함되어 있는지 궁금한 경우가 있지만 포함 할 필요가 없다고 생각합니다.

답변

1

onCreate에서 mReciever는 null 개체 (이전에 할당하지 않은 경우)이므로 수신기를 등록하기 전에 할당해야합니다. 이 부분 변경 :

mReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     temp= intent.getExtras().getParcelable("Location"); 
    } 
}; 

//Listen for service to send location data 
LocalBroadcastManager.getInstance(this) 
    .registerReceiver(mReceiver, new IntentFilter("UpdateLocation"));