0

저는 Android에서 새롭고 새로운 것이므로 컨텍스트와 관련된 다음 작업을 수행하는 데 어려움이 있습니다.Context 객체를 클래스 Extenging 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 애플리케이션에 표시하는 것은 무엇입니까?

는 나는 내가 의 GetResources() 방법을 사용할 수 있습니다 활동 클래스로 상황을 얻기 위해 알고있다.

내 문제는 조각을 exetends 클래스로 컨텍스트를 얻어야한다는 것입니다.

나는 이런 식으로 뭔가가 :

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; 
} 

내 문제를 그 이전의 조각 클래스에서 나는 방법을 호출 할 때 : (() 출력

ImgUtility.createRankingImg(getResources(), 3) 

그것에 의 GetResources 통과를 그 생각은 내게 , IDE는 다음과 같은 오류 메시지가 나타납니다 :

첫 번째 인수 유형이 잘못되었습니다. 찾았습니다 'android.content.res.Resources', 이 필요 : 'android.content.Context'가

그래서이 날 것으로 보인다있는 조각 아닌 활동 확장하는 클래스getResources() 메서드는 리소스 개체를 대신합니다. 컨텍스트 개체 (작업 클래스로 완료 됨)입니다. 사실입니까? 왜?

조각을 확장하는 클래스에서 컨텍스트를 얻으려면 어떻게해야합니까? 그리고 정확히 Android 앱의 컨텍스트를 나타내는 것은 무엇입니까? 내가 뭘 놓치고 있니? 어떻게하면이 문제를 해결할 수 있습니까?

+2

'getActivity()' –

+0

나에게 "getActivity()"를 해결할 수 없습니까? 왜? – AndreaNobili

+1

'getActivity()'는'Fragment'에 정의되어 있으므로 분명히'Fragment'가 아니거나 오타가 있습니다. – laalto

답변

0

Activity 클래스의 컨텍스트 개체를 전달할 수 있도록 Context를 매개 변수로 사용하는 초기 함수를 만듭니다.

+0

조각 생성자는 인수를 취하지 않아야합니다. – laalto

관련 문제