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