2014-10-13 4 views
0

개발중인 Android 앱에 대한 도움이 필요합니다. C, perl 및 어셈블리에 대한 많은 경험이 있지만 완전한 Java 놈입니다.반복 이벤트 충돌 앱

간단히 말해서, 나는 실행중인 시계를 표시하는 응용 프로그램을 작성하고 있습니다. 시계 위젯에 내장되어 있다는 것을 이해합니다. 그러나 이것을 초당 1 회 업데이트하는 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); 
    } 

} 
+2

"setText를 생성 한 스레드 외부에서 setText를 호출하는 것과 관련해." 너의 경험 수준의 누군가를 위해 다소 모호하다 (그리고 거의 쓸모가 없다). 전체 오류 메시지의 스택 추적/로그 cat 게시를 고려할 수 있습니다. – MarsAtomic

답변

0

시도해보십시오.

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

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

     },0, 1000); 

당신은 메인 UI 스레드가 아닌 다른 스레드에서 뷰 객체를 변경하기 위해 runOnUiThread를 호출해야합니다.

+0

Bri6ko, 완벽하게 작동했습니다. 고마워. – vne147