我的计时器正在执行的方法一直失败

发布于 2024-12-21 07:52:47 字数 5700 浏览 2 评论 0原文

我使用 Timer 类每 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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想你的星星会说话 2024-12-28 07:52:47

您的异常意味着您正在尝试从非 UI 线程修改用户界面,在 Android 中,可以更新用户界面的唯一线程UIThread,因此要解决该问题,您应该强制 UIThread 更改线程内的 UserInterface,如下所示:

//Code of your Method TimerMethod
//......
YourActivity.this.runOnUiThread(new Runnable() {
    public void run() {
        //your changes on your UserInterface ( TextViews in your Case ) .  
    }
});

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 ) .  
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文