2013-04-09 27 views
0

我遇到了例外NetworkOnMainThreadException異常。Asynctask配置發送特定參數

所以我必須採取一個功能,以一個異類,問題是,我必須發送兩個參數,這不是發送和我收到它們。

這些都是需要發送的參數,這樣的AsyncTask接受他們

​​

這是我需要提請的AsyncTask

public void SetImages(TypedArray array, boolean reflected){ 

    final int reflectionGap = 4; 

//   Drawable[] drawables = new Drawable[array.length()]; 
//   mImages = new CarouselImageView[array.length()]; 
    mImages = new CarouselImageView[MainActivity.aL_home.size()]; 
    Log.e("TAG", "SIZE OF: "+MainActivity.aL_home.size()); 
    for(int i = 0; i< MainActivity.aL_home.size(); i++) 
    { 
     try { 
      Log.e("TAG","url: "+MainActivity.aL_home.get(i).getUrl_imagen()); 
//    drawables[i] = array.getDrawable(i); 
//    Bitmap originalImage = ((BitmapDrawable)drawables[i]).getBitmap(); 
      Bitmap originalImage = BitmapFactory.decodeStream((InputStream)new URL(MainActivity.aL_home.get(i).getUrl_imagen()).getContent()); 
      if(reflected){ 
       int width = originalImage.getWidth(); 
       int height = originalImage.getHeight(); 

       // This will not scale but will flip on the Y axis 
       Matrix matrix = new Matrix(); 
       matrix.preScale(1, -1); 

       // Create a Bitmap with the flip matrix applied to it. 
       // We only want the bottom half of the image 
       Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, 
         height/2, width, height/2, matrix, false); 

       // Create a new bitmap with same width but taller to fit 
       // reflection 
       Bitmap bitmapWithReflection = Bitmap.createBitmap(width, 
         (height + height/2), Config.ARGB_8888); 

       // Create a new Canvas with the bitmap that's big enough for 
       // the image plus gap plus reflection 
       Canvas canvas = new Canvas(bitmapWithReflection); 
       // Draw in the original image 
       canvas.drawBitmap(originalImage, 0, 0, null); 
       // Draw in the gap 
       Paint deafaultPaint = new Paint(); 
       canvas.drawRect(0, height, width, height + reflectionGap, 
         deafaultPaint); 
       // Draw in the reflection 
       canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, 
         null); 

       // Create a shader that is a linear gradient that covers the 
       // reflection 
       Paint paint = new Paint(); 
       LinearGradient shader = new LinearGradient(0, 
         originalImage.getHeight(), 0, 
         bitmapWithReflection.getHeight() + reflectionGap, 
         0x70ffffff, 0x00ffffff, TileMode.CLAMP); 
       // Set the paint to use this shader (linear gradient) 
       paint.setShader(shader); 
       // Set the Transfer mode to be porter duff and destination in 
       paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 
       // Draw a rectangle using the paint with our linear gradient 
       canvas.drawRect(0, height, width, 
         bitmapWithReflection.getHeight() + reflectionGap, paint); 

       originalImage = bitmapWithReflection; 
      } 

      CarouselImageView imageView = new CarouselImageView(mContext); 
      imageView.setImageBitmap(originalImage); 
      imageView.setIndex(i); 

      ////////imageView.setLayoutParams(new CarouselOld.LayoutParams(120, 180)); 
      ////////imageView.setScaleType(ScaleType.MATRIX); 
      mImages[i] = imageView; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

} 

回答

1

NetworkOnMainThreadException

這種異常的,如果你試圖操縱和改變從不與主(UI)線程同步的後臺線程的UI被拋出。

的AsyncTask提供了幾個方法,你可以更新你的UI:

  • onPreExecute
  • onProgressUpdate
  • onPostExecute

,您應該只在提到的方法進行任何UI更新。

這是我需要提請的AsyncTask

所以通過的AsyncTask即構造你的方法進入的AsyncTask類並把所需的參數(TypedArray和布爾)代碼

Task task = new Task(arr, boolVariable); 
task.execute(); 
0

所以問題是如何將參數傳遞給AsyncTask?

格式

private class MyTask extends AsyncTask<Params, Progress, Result> { ... } 

http://developer.android.com/reference/android/os/AsyncTask.html

所以,你可以定義你的AsyncTask像

private class SetImagesTask extends AsyncTask<TypedArray, Void, Void> { 
    protected Void doInBackground(TypedArray... typedArrays) { 
     TypedArray array = typedArrays[0]; 
     ... 
    } 
    ... 
} 

,並調用它像

new SetImagesTask.execute(array); 

對於一個布爾值,你Ç應該使用主類字段或靜態變量,所以你可以在execute(array)之前設置它。或者,您可以將布爾值封裝到第二個TypedArray中,並將它們的數組傳遞給execute(new TypedArray [] {array,booleanArray});發生

0

NetworkOnMainThread異常,因爲你正在運行主UI Thread.This網絡相關的操作只拋出應用針對蜂窩SDK或更高

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

您應該使用的AsyncTask。

http://developer.android.com/reference/android/os/AsyncTask.html

在的onCreate()

new TheTask().execute(); 

您也可以通過類似URL參數的AsyncTask的構造和使用相同的doInBackground()

class TheTask extends AsyncTask<Void,Void,Void> 
{ 
protected void onPreExecute() 
{   super.onPreExecute(); 
     //display progressdialog. 
} 

protected void doInBackground(Void ...params)//return result here 
{ 
    //http request. do not update ui here 

    return null; 
} 

protected void onPostExecute(Void result)//result of doInBackground is passed a parameter 
{  
     super.onPostExecute(result); 
     //dismiss progressdialog. 
     //update ui using the result returned form doInbackground() 
} 
} 

當執行異步任務,任務經過4個步驟:

  1. onPreExecute(),在任務執行前在UI線程上調用。此步驟通常用於設置任務,例如通過在用戶界面中顯示進度條。

  2. doInBackground(Params ...),在onPreExecute()完成執行後立即在後臺線程上調用。此步驟用於執行可能需要很長時間的後臺計算。異步任務的參數傳遞給此步驟。計算結果必須通過該步驟返回並返回到最後一步。此步驟還可以使用publishProgress(Progress ...)發佈一個或多個進度單元。這些值在onProgressUpdate(Progress ...)步驟中發佈在UI線程上。

  3. onProgressUpdate(Progress ...),在調用publishProgress(Progress ...)後在UI線程上調用。執行的時間是未定義的。此方法用於在後臺計算仍在執行時在用戶界面中顯示任何形式的進度。例如,它可以用來爲進度條設置動畫效果或在文本字段中顯示日誌。

  4. onPostExecute(Result),在後臺計算完成後在UI線程上調用。後臺計算的結果作爲參數傳遞給此步驟。