2013-10-23 3 views
0

안드로이드 응용 프로그램에서 asynctask runner를 사용하는 동안, 텍스트보기에서 gps 좌표를 표시하기 위해 매 5 초마다 while 루프에서 asynctaskrunner를 실행하는 중 일부가 발생했습니다.asynctaskrunner 안드로이드 응용 프로그램에서 오류가 발생했습니다.

package com.example.gpsproject;

import android.content.Context; 
import android.location.Location; 
import android.os.AsyncTask; 
import android.widget.TextView; 
public class AsyncTaskRunner extends AsyncTask<Void,Void,Void> { 


    private final Context mContext; 
    TextView latitude,longitude; 


public AsyncTaskRunner(Context c,TextView lat,TextView lon) { 
    // TODO Auto-generated constructor stub 
    mContext = c; 
    latitude = lat; 
    longitude = lon; 

} 
Location a = new Location("zfcdha"); 
String lonii,latii; 


private void sleep(int i) { 
    // TODO Auto-generated method stub 

} 


protected void onPostExecute() { 


} 


@Override 
protected void onPreExecute() { 

} 



protected Void doInBackground(Void... params) { 

    try { 
     GPSTracker mytracker = new GPSTracker(mContext); 

    while(true){ 
    latii = "" + a.getLatitude(); 
    lonii = "" + a.getLongitude(); 
    latitude.setText(latii); 
    longitude.setText(lonii); 
      sleep(5000); 
    } 





    } catch (Exception e) { 
    e.printStackTrace(); 
; 
    } 
; 
return null; 


} 
} 
+0

백그라운드 스레드에서'setText()'를 호출하고 있습니다. 그러지 마. – Geros

답변

0

메인 스레드에서 백그라운드 스레드가 아닌 UI를 업데이트해야합니다. 당신은 당신이 아래처럼 runOnUiThread 방법을 사용할 수 있습니다 value.or을 설정하는 메시지를 보낼 수 처리기를 사용할 수 있습니다

protected Void doInBackground(Void... params) { 

    try { 
     GPSTracker mytracker = new GPSTracker(mContext); 

    while(true){ 
    latii = "" + a.getLatitude(); 
    lonii = "" + a.getLongitude(); 
    currentActivity.this.runOnUiThread(new Runnable() {  
      public void run()  
      {  
       latitude.setText(latii); 
       longitude.setText(lonii); 
      }  

     }); 
    sleep(5000); 
    } 

    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
    return null; 

} 
1

onPostExecute (결과) UI 스레드 onPreExecute()에서 실행은 UI 스레드에서 실행

doInBackground (void ... params)는 자체 스레드에서 실행됩니다.

다른 스레드에서 UI를 절대로 변경하면 안되며 메시지를 사용하는 것이 좋습니다.

관련 문제