Reputation: 4055
I am using the Timer Class to execute a function every 2 mins.
The code in my TimerMethod is failing but when it was part of onCreat it worked perfectly. I do notice it is failing when it gets to updating text fields in my layout.
Code is below:
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);
}
in the same class my 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);
}
I do not get an error everytime in my logCat but I did see this come through on one of my test.
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)
Upvotes: 1
Views: 479
Reputation: 24181
your Exception means that you are trying to modify the User Interface from a non UI Thread, in Android , the Only Thread that can update the User Interface is the UIThread
, so to fixe the problem , you should force the UIThread
to change the UserInterface inside your Thread like this :
//Code of your Method TimerMethod
//......
YourActivity.this.runOnUiThread(new Runnable() {
public void run() {
//your changes on your UserInterface ( TextViews in your Case ) .
}
});
Upvotes: 2