2015-02-03 5 views
0

AsyncTask의 ProgressBar이 충돌합니다. 이 코드는 필수적으로 URL에서 이미지를로드하고 진행 중에 진행률 회 전자를 표시합니다. 그룹의 첫 번째 이미지는 잘로드되지만 그 다음에는 앱이 다운됩니다.AsyncTask의 ProgressBar이 충돌 함

public class LoadImageTask extends AsyncTask<String, Void, Bitmap> { 
ImageView bmImage; 
ProgressBar progressBar; 

public LoadImageTask(ImageView bmImage, ProgressBar progressBar) { 
    this.bmImage = bmImage; 
    this.progressBar = progressBar; 
} 

protected Bitmap doInBackground(String... urls) { 
    progressBar.setVisibility(View.VISIBLE); 
    String urldisplay = urls[0]; 
    Bitmap scaledImage = null; 
    try { 
     InputStream in = new java.net.URL(urldisplay).openStream(); 
     Bitmap image = BitmapFactory.decodeStream(in); 

     scaledImage = Bitmap.createScaledBitmap(image, 380, 250, false); 
    } catch (Exception e) { 
     Log.e("Error", e.getMessage()); 
     e.printStackTrace(); 
    } 
    return scaledImage; 
} 

protected void onPostExecute(Bitmap result) { 
    progressBar.setVisibility(View.GONE); 
    bmImage.setImageBitmap(result); 
} 

}

콘솔 출력 :

02-03 02:45:44.014 24125-24375/simplewall.ryandushane.com.simplewall E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #5 
    Process: simplewall.ryandushane.com.simplewall, PID: 24125 
    java.lang.RuntimeException: An error occured while executing doInBackground() 
      at android.os.AsyncTask$3.done(AsyncTask.java:300) 
      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 
      at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 
      at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
      at java.lang.Thread.run(Thread.java:818) 
    Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
      at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6247) 
      at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:867) 
      at android.view.View.requestLayout(View.java:17364) 
      at android.view.View.requestLayout(View.java:17364) 
      at android.view.View.requestLayout(View.java:17364) 
      at android.view.View.requestLayout(View.java:17364) 
      at android.view.View.requestLayout(View.java:17364) 
      at android.view.View.requestLayout(View.java:17364) 
      at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:360) 
      at android.view.View.requestLayout(View.java:17364) 
      at android.widget.AbsListView.requestLayout(AbsListView.java:1975) 
      at android.view.View.requestLayout(View.java:17364) 
      at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:360) 
      at android.view.View.requestLayout(View.java:17364) 
      at android.view.View.setFlags(View.java:9633) 
      at android.view.View.setVisibility(View.java:6663) 
      at android.widget.ProgressBar.setVisibility(ProgressBar.java:1563) 
      at simplewall.ryandushane.com.simplewall.LoadImageTask.doInBackground(LoadImageTask.java:27) 
      at simplewall.ryandushane.com.simplewall.LoadImageTask.doInBackground(LoadImageTask.java:17) 
      at android.os.AsyncTask$2.call(AsyncTask.java:288) 
      at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
            at java.lang.Thread.run(Thread.java:818) 

답변

2

CalledFromWrongThreadException : 당신이 progressBar.setVisibility 메소드를 호출하기 때문에 뷰 계층 구조를 실행 doInBackground에서의 전망

만질 수 생성 만 원래의 스레드 비 - ui 스레드.

재정의 진행률 표시 줄의 가시성 변경 메인 UI 스레드에서 실행 AsyncTaskonPreExecute() 방법 : onPreExecute

1

당신은 doInBackgroundprogressBar.setVisibility(View.VISIBLE);을 설정할 수 없습니다. 그 다른 스레드

또는

protected Bitmap doInBackground(String... urls) { 
    runOnUiThread (new Runnable() { 

         @Override 
         public void run() { 

          progressBar.setVisibility(View.VISIBLE); 

         } 
        }); 

    String urldisplay = urls[0]; 
    Bitmap scaledImage = null; 
    try { 
     InputStream in = new java.net.URL(urldisplay).openStream(); 
     Bitmap image = BitmapFactory.decodeStream(in); 

     scaledImage = Bitmap.createScaledBitmap(image, 380, 250, false); 
    } catch (Exception e) { 
     Log.e("Error", e.getMessage()); 
     e.printStackTrace(); 
    } 
    return scaledImage; 
} 
1

이동 progressBar.setVisibility(View.VISIBLE);를 onPreExecute (이 progressBar.setVisibility(View.VISIBLE)을 넣어) AsyncTask를 방법이 제거 doInBackground 메소드의 행.

0

@Override 
protected void onPreExecute() { 
    progressBar.setVisibility(View.VISIBLE); 
}