我使用Timer Class每2分钟执行一次函数。
我的TimerMethod中的代码失败但是当它成为onCreat的一部分时它完美地运行了。我注意到它在更新布局中的文本字段时失败了。
代码如下:
public class MeetingManager extends ListActivity {
private static final String TAG = "MyApp";
public static final String PREFS_NAME = "MyAppData";
private Timer myTimer;
/** Called when the activity is first created. */
//private PendingIntent mAlarmSender;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
TimerMethod();
}
}, 0, 10000);
}
在同一个班级我的TimerMethod:
protected void TimerMethod() {
Log.d(TAG, "NEW IN TIMER");
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String items = settings.getString("RoomId", "MISSING");
Map<String, String> maproom = new HashMap<String, String>();
maproom.put("145", "dddd");
maproom.put("110", "vvvv");
maproom.put("148", "ddddd");
maproom.put("45", "Pfgdfgdfgdf");
String roomName = maproom.get(items);
Log.d(TAG, roomName);
TextView currentRoomName = (TextView) this.findViewById(R.id.RoomTitle);
currentRoomName.setText(roomName);
ImageView b=(ImageView)findViewById(R.id.b);
b.setOnClickListener(listener);
Date anotherCurDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("EEEE', 'MMMM dd', ' yyyy");
String formattedDateString = formatter.format(anotherCurDate);
TextView currentRoomDate = (TextView) this.findViewById(R.id.CurrentDate);
currentRoomDate.setText(formattedDateString);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
String xml = XMLfunctions.getXML(items);
Document doc = XMLfunctions.XMLfromString(xml);
/*int numResults = XMLfunctions.numResults(doc);
if((numResults <= 0)){
Toast.makeText(MeetingManager.this, "NOT GETTING XML", Toast.LENGTH_LONG).show();
finish();
} */
Element docElem = doc.getDocumentElement();
NodeList nodes = (NodeList)docElem.getElementsByTagName("meeting_item");
int node_number = nodes.getLength();
String node_final = String.valueOf(node_number);
Log.d(TAG, node_final);
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("time", XMLfunctions.getValue(e, "time"));
map.put("endtime", XMLfunctions.getValue(e, "endtime"));
map.put("name", XMLfunctions.getValue(e, "meeting_name"));
map.put("hostname", XMLfunctions.getValue(e, "host_name"));
mylist.add(map);
}
ListAdapter adapter = new SimpleAdapter(MeetingManager.this, mylist , R.layout.listlayout,
new String[] {"time","endtime", "name", "hostname" },
new int[] { R.id.time, R.id.endtime, R.id.meeting_name, R.id.host });
setListAdapter(adapter);
}
我的logCat中每次都没有出现错误,但我确实在我的测试中看到了这个错误。
12-14 09:39:29.584: ERROR/AndroidRuntime(572): FATAL EXCEPTION: Timer-0
12-14 09:39:29.584: ERROR/AndroidRuntime(572): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-14 09:39:29.584: ERROR/AndroidRuntime(572): at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
12-14 09:39:29.584: ERROR/AndroidRuntime(572): at android.view.ViewRoot.requestLayout(ViewRoot.java:594)
12-14 09:39:29.584: ERROR/AndroidRuntime(572): at android.view.View.requestLayout(View.java:8125)
12-14 09:39:29.584: ERROR/AndroidRuntime(572): at android.view.View.requestLayout(View.java:8125)
12-14 09:39:29.584: ERROR/AndroidRuntime(572): at android.view.View.requestLayout(View.java:8125)
12-14 09:39:29.584: ERROR/AndroidRuntime(572): at android.view.View.requestLayout(View.java:8125)
12-14 09:39:29.584: ERROR/AndroidRuntime(572): at android.widget.TextView.checkForRelayout(TextView.java:5378)
12-14 09:39:29.584: ERROR/AndroidRuntime(572): at android.widget.TextView.setText(TextView.java:2688)
12-14 09:39:29.584: ERROR/AndroidRuntime(572): at android.widget.TextView.setText(TextView.java:2556)
12-14 09:39:29.584: ERROR/AndroidRuntime(572): at android.widget.TextView.setText(TextView.java:2531)
12-14 09:39:29.584: ERROR/AndroidRuntime(572): at com.MeetingManager.MeetingManager.TimerMethod(MeetingManager.java:171)
12-14 09:39:29.584: ERROR/AndroidRuntime(572): at com.MeetingManager.MeetingManager$2.run(MeetingManager.java:68)
12-14 09:39:29.584: ERROR/AndroidRuntime(572): at java.util.Timer$TimerImpl.run(Timer.java:289)
答案 0 :(得分:2)
您的例外意味着您尝试修改来自非UI线程的用户界面,在Android中,可以更新用户界面的唯一线程是{ {1}},为了解决这个问题,你应该强制UIThread
更改你的线程内的UserInterface,如下所示:
UIThread