2016-08-11 16 views
0

私はcontentobserverのクラスを持っています。contentobserverからbroasdcastメッセージを送信します。しかし、コールアプリがクラッシュすると、logcateコンテキストが表示されますnullcontentresolverからメッセージを送信する方法を教えてください。 ここに私のコードです:androidはcontentobserverからブロードキャストメッセージを送信します

public class SettingsContentObserver extends ContentObserver { 

    Context context; 
    public SettingsContentObserver(Handler handler) { 
     super(handler); 
    } 

    @Override 
    public boolean deliverSelfNotifications() { 
     return super.deliverSelfNotifications(); 
    } 

    @Override 
    public void onChange(boolean selfChange) { 
     super.onChange(selfChange); 

     //Profile1Activity.profile1(context); 

     Intent i = new Intent("settingschanged"); 
     context.sendBroadcast(i); 
    } 
} 
+0

どこにどのようにコンテキストを割り当てるのですか? – Abbas

+0

これは完全なコード –

+0

コンテキストはnullになります。まだコンテキストを指定していない場合は、nullオブジェクト参照でメソッドを呼び出そうとしています。コンテキストにある値を割り当ててください! – Abbas

答えて

0

はあなたがContentObserverへのアプリケーションコンテキストを渡すことを試みることができますか?

public class SettingsContentObserver extends ContentObserver { 

    Context mContext; 
    public SettingsContentObserver(Handler handler, Context context) { 
     super(handler); 
     this.mContext = context; 
    } 

    @Override 
    public boolean deliverSelfNotifications() { 
     return super.deliverSelfNotifications(); 
    } 

    @Override 
    public void onChange(boolean selfChange) { 
     super.onChange(selfChange); 

     //Profile1Activity.profile1(context); 

     Intent i = new Intent("settingschanged"); 
     mContext.sendBroadcast(i); 
    } 
} 
+0

その働きのおかげで –

+0

素晴らしい!あなたが解決策に満足しているなら、答えを受け入れてください。 – Progen

関連する問題