2017-11-09 19 views
0

TextViewを更新しようとしています。Android Dev:UIスレッドでTimerTaskを実行

このメソッドは私のメインクラスにあり、onCreate()で呼び出されます。

私はとすぐに30秒に達すると、それがクラッシュしたのTextViewを更新しようとしているよう..無駄にrunOnUiThread()を使用して

を試してみました!

すべてのヘルプは高く評価されています

public void updateDisplay() { 
    Timer timer = new Timer(); 


    timer.schedule(new TimerTask() { 


         public void run() { 
          Calendar c = Calendar.getInstance(); 
          int mYear = c.get(Calendar.YEAR); 
          int mMonth = c.get(Calendar.MONTH); 
          int mDay = c.get(Calendar.DAY_OF_MONTH); 
          int mHour = c.get(Calendar.HOUR_OF_DAY); 
          int mMinute = c.get(Calendar.MINUTE); 

          textView3.setText(new StringBuilder() 
            // Month is 0 based so add 1 
            .append(mDay).append("/") 
            .append(mMonth + 1).append("/") 
            .append(mYear) 
            .append(" - ") 
            .append(mHour) 
            .append(":") 
            .append(mMinute)); 
         } 
        } 

      ,0,30000);//Update text every 30 seconds 
} 

とエラーは次のようになります。

E/AndroidRuntime: FATAL EXCEPTION: Timer-0 
       Process: ggrgroup.com.workforceapp, PID: 21879 
       android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
        at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7261) 
        at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1100) 
        at android.view.ViewGroup.invalidateChild(ViewGroup.java:5219) 
        at android.view.View.invalidateInternal(View.java:12900) 
        at android.view.View.invalidate(View.java:12860) 
        at android.view.View.invalidate(View.java:12844) 
        at android.widget.TextView.checkForRelayout(TextView.java:7459) 
        at android.widget.TextView.setText(TextView.java:4390) 
        at android.widget.TextView.setText(TextView.java:4247) 
        at android.widget.TextView.setText(TextView.java:4222) 
        at ggrgroup.com.workforceapp.MainActivity$1.run(MainActivity.java:77) 
        at java.util.Timer$TimerImpl.run(Timer.java:284) 
+0

投稿クラッシュエラーがlogcatに記録されました! – pleft

+0

非UIスレッドからView要素を更新しようとしているため、クラッシュしています。ハンドラを使用する必要があります。 https://developer.android.com/training/multiple-threads/communicate-ui.htmlを参照してください。 – Ezio

+0

ありがとうございました!すべてソート! UIスレッドを正しく実行する:) –

答えて

0
public void updateDisplay() { 
     Timer timer = new Timer(); 
     timer.schedule(new TimerTask() { 

    @Override 
    public void run() { 
     // Your logic here... 

     // When you need to modify a UI element, do so on the UI thread. 
     // 'getActivity()' is required as this is being ran from a Fragment. 
     getActivity().runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       // This code will always run on the UI thread, therefore is safe to modify UI elements. 
       myTextBox.setText("my text"); 
      } 
     }); 
    } 
}, 0, 30000); // End of your timer code. 

} 
0

android.view.ViewRootImpl $ CalledFromWrongThreadException:ビュー階層を作成しただけ元のスレッドが触れることができますその見解。

あなたはそれを試すスレッド

でtextView3を編集するので:

ます。public void updateDisplay(){ タイマータイマー=新しいTimer();

timer.schedule(new TimerTask(){ 


    public void run() { 

     runOnUiThread(new Runnable(){ 
      @Override 
      public void run() { 
       Calendar c = Calendar.getInstance(); 
       int mYear = c.get(Calendar.YEAR); 
       int mMonth = c.get(Calendar.MONTH); 
       int mDay = c.get(Calendar.DAY_OF_MONTH); 
       int mHour = c.get(Calendar.HOUR_OF_DAY); 
       int mMinute = c.get(Calendar.MINUTE); 

       textView3.setText(new StringBuilder() 
        // Month is 0 based so add 1 
        .append(mDay).append("/") 
        .append(mMonth + 1).append("/") 
        .append(mYear) 
        .append(" - ") 
        .append(mHour) 
        .append(":") 
        .append(mMinute)); 
      } 
     }); 

    } 
} 

, 0, 30000);//Update text every 30 seconds 

}

0

@Ezio

Handler handler = new Handler(Looper.getMainLooper()); 

private Runnable runnable = new Runnable() { 

    public void run() { 
     Calendar c = Calendar.getInstance(); 
     int mYear = c.get(Calendar.YEAR); 
     int mMonth = c.get(Calendar.MONTH); 
     int mDay = c.get(Calendar.DAY_OF_MONTH); 
     int mHour = c.get(Calendar.HOUR_OF_DAY); 
     int mMinute = c.get(Calendar.MINUTE); 

     textView3.setText(new StringBuilder() 
      // Month is 0 based so add 1 
      .append(mDay).append("/") 
      .append(mMonth + 1).append("/") 
      .append(mYear) 
      .append(" - ") 
      .append(mHour) 
      .append(":") 
      .append(mMinute)); 

     //repeat every 30secs 
     handler.postDelayed(this, 30000); 
    } 
} 

handler.postDelayed(runnable, 30000); 
  • Looper.getMainLooper()で述べたように、UIスレッド
  • handler.postDelayed(this, 30000);上で実行可能を実行するハンドラを使用し、それはすべての30secs
  • を繰り返すことになります
関連する問題