私はタイマークラスを使用して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のエラー毎回を得ることはありませんが、私は、これは1に通ってくる見ました私のテストの。
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)
ポストあなたのlogcatエラー –
あなたのタイマーを始めましたの? – Houcine
今すぐログアウトする申し訳ありませんが私はそれが私のポストにあったと思った。 – Denoteone