重复事件崩溃的应用程序

时间:2014-10-13 20:10:52

标签: java android timer timertask settext

我正在开发的Android应用程序需要一些帮助。我有很多C,perl和汇编的经验,但我是一个完整的Java noob。

简而言之,我正在编写一个显示运行时钟的应用程序。我知道有内置的时钟小部件,但我更喜欢使用TextView对象来实现这一点,我每秒更新一次。但是,当我设置重复事件并尝试使用setText方法时,我的应用程序崩溃了。关于从创建它的线程外部调用setText的事情。

我搜索了一下,看到了一些关于使用处理程序的建议,但我没有成功使用这种方法。有人可以指出我做错了什么或建议更好的方法吗?

我的代码如下。我正在使用Android Studio 0.8.9并在我的LG G2上运行应用程序。

提前感谢您提供的任何帮助。

package com.joshrocks.betclock;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import android.graphics.Typeface;
import android.widget.TextClock;
import android.widget.TextView;

import java.util.Calendar;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;


public class BET_Clock extends Activity {

    //TextView and TextClock definitions
    TextView lab_GMT;
    TextView lab_LCL;
    TextView lab_BET;
    TextView lab_BJD;
    TextView Day_LCL;
    TextView Slash_LCL;
    TextClock time_LCL;
    TextView Day_GMT;
    TextView Slash_GMT;
    TextClock time_GMT;

    //Calendar declarations
    Calendar GMT_Calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    Calendar LCL_Calendar = Calendar.getInstance();

    //Call GMT and LCL Day of the year
    int GMT_dayOfYear = GMT_Calendar.get(Calendar.DAY_OF_YEAR);
    int LCL_dayOfYear = LCL_Calendar.get(Calendar.DAY_OF_YEAR);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bet__clock);
        Typeface dot_matrix=Typeface.createFromAsset(getAssets(),"fonts/led_counter-7.ttf");

        //Label font definitions
        lab_LCL = (TextView) findViewById(R.id.Label_LCL);
        lab_LCL.setTypeface(dot_matrix);
        lab_GMT = (TextView) findViewById(R.id.Label_GMT);
        lab_GMT.setTypeface(dot_matrix);
        lab_BET = (TextView) findViewById(R.id.Label_BET);
        lab_BET.setTypeface(dot_matrix);
        lab_BJD = (TextView) findViewById(R.id.Label_BJD);
        lab_BJD.setTypeface(dot_matrix);

        //GMT Time field font definitions
        Day_GMT = (TextView) findViewById(R.id.Day_GMT);
        Day_GMT.setTypeface(dot_matrix);
        Day_GMT.setText(String.valueOf(GMT_dayOfYear));
        Slash_GMT = (TextView) findViewById(R.id.Slash_GMT);
        Slash_GMT.setTypeface(dot_matrix);
        time_GMT = (TextClock) findViewById(R.id.Time_GMT);
        time_GMT.setTypeface(dot_matrix);

        //LCL Time field font definitions
        Day_LCL = (TextView) findViewById(R.id.Day_LCL);
        Day_LCL.setTypeface(dot_matrix);
        Day_LCL.setText(String.valueOf(LCL_dayOfYear));
        Slash_LCL = (TextView) findViewById(R.id.Slash_LCL);
        Slash_LCL.setTypeface(dot_matrix);
        time_LCL = (TextClock) findViewById(R.id.Time_LCL);
        time_LCL.setTypeface(dot_matrix);


         Timer timer = new Timer();
         timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                UpdateTimes();
            }

         },0, 1000);

    }//end OnCreate


    public void UpdateTimes() {
        LCL_dayOfYear += 1;
        Day_LCL.setText(String.valueOf(LCL_dayOfYear));
    }//end UpdateTimes


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.bet__clock, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

1 个答案:

答案 0 :(得分:0)

试试这个。

Timer timer = new Timer();
         timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                       UpdateTimes();
                    }
                });
            }

         },0, 1000);

您需要调用runOnUiThread才能从主UI线程以外的其他线程更改视图对象。