2012-02-09 11 views
0

私の外部ブロードキャストレシーバクラスを私のサービスに変更したばかりなので、いくつかのアンドロイドメソッドは静的コンテキストでは使用できませんでした。今すぐエラーが表示されるComponentInfo {com ...}のアクティビティをインスタンス化できません:java.lang.NullPointerException。どのように修正することが可能ですか?以下は、ネストされたBroadcastReceiverクラスのための私のコードです。内部ブロードキャストレシーバクラスをインスタンス化

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方法である:ここでは、以下の

public class ActivityX extends Activity{ 

private BroadcastReceiver receiver = security365Service.new NotifyServiceReceiver(); 

は私がオンラインいくつかのソースを見た後に変更私のmainfestです:

<receiver android:name="com.milanix.services.ServiceX$ReceiverX" android:enabled="true"> 
    </receiver> 

申し訳ありませんが、私の質問はダムです。

答えて

2

ここでは通常の内部クラスは使用できません。 static内部クラスである必要があり、元の問題に戻ることができます。だから、あなたはあなたの "いくつかのアンドロイドメソッドは静的コンテキストで使用できない"問題を解決する必要があります。

+0

しかし、私は共有のPrefsを持っています。また、ブロードキャスト受信者は、共有されたプリファレンスをコミットするメソッドを変更します。これをどうやって解決するのですか? ContextWrapperタイプのgetSharedPreferences(String、int)へのstatic参照を作成できません。 – Milan

+0

@Milanix: 'OnReceive()'に与えられた 'Context'を使用して' SharedPreferences'にアクセスしてください。あるいは 'BroadcastReceiver'コントロールを' startService() 'を介して' IntentService'に渡し、 'SharedPreferences'を更新させます。 – CommonsWare

+0

あなたのコメントのコード例を教えてください。 – Milan

関連する問題