重复事件崩溃应用程序



我需要一些关于我正在开发的Android应用程序的帮助。我在C、perl和汇编方面有丰富的经验,但我是一个完全的Java新手。

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

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

我的代码在下面。我正在使用安卓工作室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);
    }
}

试试这个。

Timer timer = new Timer();
         timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                       UpdateTimes();
                    }
                });
            }
         },0, 1000);

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

最新更新