2012-01-07 3 views
0

c2dmイベントがトリガされるたびにレイアウト要素(TextArea)を更新したい場合は、イベントリスナからビューを取得するにはどうすればよいですか?c2dmイベントリスナーからのアクティビティテキストエリアの更新

イベントリスナー、関心の2行は、メッセージ処理の下にある、テキストエリアのコメント

public class C2DMMessageReceiver extends BroadcastReceiver { 
     @Override 
       public void onReceive(Context context, Intent intent) { 
        String action = intent.getAction(); 
        Log.w("C2DM", "Message Receiver called"); 
        if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) { 
         Log.w("C2DM", "Received message"); 
         final String payload = intent.getStringExtra("payload"); 
         Log.d("C2DM", "dmControl: payload = " + payload); 

         // Message handling, update text area 
         if(payload.equals("DataUpdate")) { 
         t=(TextView) context.findViewById(R.id.gameConsoleText); 
    t.setText("evt rcvd!"); 
         } 
        } 
       } 
    } 

を更新するこの行にエラーがある、私はコンテキスト

t=(TextView) context.findViewById(R.id.gameConsoleText); 

からfindViewByIDを取得することはできません何かご意見は?ありがとう

答えて

1

findViewById()はアクティビティにのみ存在します!

しかし、あなたは、このようなLayoutInflaterを使用することができます。

LayoutInflater myInflater = LayoutInflater.from(context); 
View myView = myInflater.inflate(R.layout.yourView, null); 
t = (TextView) myView.findViewById(R.id.gameConsoleText); 

しかし、あなたはおそらく、あなたのactivitiy以内にUIを更新する意図を上げる必要があります。

関連する問題