我想在触发c2dm事件时更新布局元素(TextArea),如何从事件监听器获取视图?
事件监听器,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!");
}
}
}
}
这一行有错误,我无法从上下文获取findViewByID
t=(TextView) context.findViewById(R.id.gameConsoleText);
有什么想法?感谢
答案 0 :(得分:1)
findViewById()仅存在于Activities!
中但你可以像这样使用LayoutInflater:
LayoutInflater myInflater = LayoutInflater.from(context);
View myView = myInflater.inflate(R.layout.yourView, null);
t = (TextView) myView.findViewById(R.id.gameConsoleText);
但是你应该提出在你的活动中更新你的用户界面的意图。