0

我是Android新手,我發現有些困難要做與Context有關的事情。如何將Context對象獲取到擴展Fragment的類中?

所以我有一個包含創造一個實用方法,並返回一個位圖圖像的實用工具類,這是我的類的代碼:

public class ImgUtility { 

    /** 
    * Method that create the images related to the difficulty of a recepy 
    * @param context 
    * @param difficulty that represent the number of chef_hat_ok into the final image 
    * @return a Bitmap representing the difficult of a recepy 
    */ 
    public static Bitmap createRankingImg(Context context, int difficulty) { 

     // Create a Bitmap image starting from the star.png into the "/res/drawable/" directory: 
     Bitmap myBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.chef_hat_ok); 


     // Create a new image bitmap having width to hold 5 star.png image: 
     Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth() * 5, myBitmap.getHeight(), Bitmap.Config.RGB_565); 

     /* Attach a brand new canvas to this new Bitmap. 
      The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: 
      1) a Bitmap to hold the pixels. 
      2) a Canvas to host the draw calls (writing into the bitmap). 
      3) a drawing primitive (e.g. Rect, Path, text, Bitmap). 
      4) a paint (to describe the colors and styles for the drawing). 
     */ 
     Canvas tempCanvas = new Canvas(tempBitmap); 

     // Draw the image bitmap into the cavas: 
     tempCanvas.drawBitmap(myBitmap, 0, 0, null); 
     tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth(), 0, null); 
     tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth() * 2, 0, null); 
     tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth() * 3, 0, null); 
     tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth() * 4, 0, null); 

     return tempBitmap; 

    } 

} 

正如你可以看到這個類包含createRankingImg ()上下文對象作爲參數並用它來創建圖像。該對象用於從資源中檢索圖像(進入BitmapFactory.decodeResource()方法)。什麼將Context對象代表Android應用程序?

我知道獲得上下文成活性I類可以使用getResources()方法

我的問題是,我不得不將上下文獲取到一個表現爲片段的類中。

我有這樣的事情:

public class ScreenSlidePageFragment extends Fragment { 

    ....................................................................... 
    ....................................................................... 
    ....................................................................... 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

     ..................................................................... 
     ..................................................................... 
     ..................................................................... 

     switch (mPageNumber + 1) { 

      case 1: 
       imgSlideView.setImageResource(R.drawable.carbonara); 
       ((TextView) rootView.findViewById(android.R.id.text1)).setText(getString(R.string.carbonara)); 

       ImageView difficultyContainerImageView1 = (ImageView) rootView.findViewById(R.id.difficultyContainer); 

       difficultyContainerImageView1.setImageDrawable(new BitmapDrawable(getResources(),  ImgUtility.createRankingImg(getApplicationContext(), 3))); 

       break; 

     ..................................................................... 
     ..................................................................... 
     ..................................................................... 

     } 

     return rootView; 
} 

我的問題是,當在之前的片段I類調用方法:

ImgUtility.createRankingImg(getResources(), 3) 

傳遞給它的getResources()輸出(我認爲給我的上下文,IDE給我以下錯誤信息:

錯誤的第一個參數類型。實測:「android.content.res.Resources」, 要求:「android.content.Context」

所以,在我看來,成延伸片段,而不是一個活動類getResources()方法返回一個資源對象而不是一個上下文對象(如完成到一個活動類)。這是真的嗎?爲什麼?

如何獲得擴展片段的類內的上下文?在Android應用中究竟代表了Context?我錯過了什麼?我該如何解決這個問題?

+2

'getActivity()' –

+0

它對我說「不能restolve getActivity()」?爲什麼? – AndreaNobili

+1

'getActivity()'是在'Fragment'中定義的,所以顯然你不在'Fragment'中或者有一個錯字或者其他東西。 – laalto

回答

0

創建一個初始函數,將Context作爲參數,以便您可以從活動類傳遞上下文對象。

+0

片段構造函數應該沒有參數。 – laalto