0
내 브로드 캐스트 리시버 클래스를 내 서비스로 변경했습니다. 일부 안드로이드 메소드는 정적 컨텍스트에서 사용할 수 없습니다. 이제 오류가 발생합니다. ComponentInfo {com ...} 활동을 인스턴스화 할 수 없습니다. java.lang.NullPointerException. 고칠 수있는 방법은 무엇입니까? 아래는 중첩 된 BroadcastReceiver 클래스에 대한 내 코드입니다.내부 브로드 캐스트 리시버 클래스를 인스턴스화합니다
public class ActivityX extends Activity{
private BroadcastReceiver receiver = security365Service.new NotifyServiceReceiver();
여기 아래
내 mainfest는 내가 온라인 어떤 소스보고 후 변경 :
<receiver android:name="com.milanix.services.ServiceX$ReceiverX" android:enabled="true">
</receiver>
public class ServiceX extends Service {
private SharedPreferences settings = getSharedPreferences(PREFS, 0);
private SharedPreferences.Editor editor = settings.edit();
private static void setEnableNotification(int command) {
if (command == 1)
enableNotification = true;
else
enableNotification = false;
editor.putBoolean("enableNotification", enableNotification);
editor.commit();
}
public static class ReceiverX extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int enableNotification = intent.getIntExtra("EnableNotification", 0);
if (enableNotification == 0)
context.
setEnableNotification(0);
else if (enableNotification == 1)
setEnableNotification(1);
}
} 아래
내가 내부 클래스를 instansiated하는 방법입니다 미안해, 내 질문이 벙어리.
하지만 공유 환경 설정이 있습니다. 그리고 브로드 캐스트 리시버는 공유 된 prefs를 커밋하는 메소드를 수정한다. 이 문제를 어떻게 해결할 수 있습니까? ContextWrapper 유형에서 getSharedPreferences (String, int) 비 정적 메소드에 대한 정적 참조를 만들 수 없습니다. – Milan
@Milanix :'OnReceive()'에 제공된'Context'를 사용하여'SharedPreferences'에 액세스하십시오. 또는,'BroadcastReceiver'가'startService()'를 통해'IntentService'에 제어권을 넘겨주고'SharedPreferences'를 갱신하게하십시오. – CommonsWare
댓글에 대한 예를 들어주세요. – Milan