0

私はAndroidで絶対に新しいです、そして、私はコンテキストに関連して次のことをするいくつかの困難を見いだしています。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が含まれて見ることができるようにContextオブジェクトをパラメータとして使用し、それを使用してイメージを作成します。このオブジェクトは、リソースから画像を取得するために使用されます(BitmapFactory.decodeResource()メソッド)。 状況のAndroidアプリケーションへの正確な表現は何ですか?

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

私の問題は、以前のフラグメントのクラスで私は(それにgetResourcesを渡す

ImgUtility.createRankingImg(getResources(), 3) 

メソッドを呼び出したときにということである)出力(それを私はコンテキストを与えると思った、IDEは私に次のエラーメッセージを与える:

第1引数型が間違っています。発見:「android.content.res.Resources」、 が必要:「android.content.Context」

だから、断片なく活動を拡張するクラスにように私には思えるgetResources()メソッドは、リソースオブジェクトを返します。コンテキストオブジェクト(アクティビティクラスの場合と同じ)です。本当ですか?どうして?

を取得するにはのクラス内にあるのフラグメント?そして、正確にはのコンテキストをAndroidアプリで表していますか?

+2

getResources()ではなくgetContext()を使用します。 注意、getContext()はnullでもかまいません。 –

+0

ContextオブジェクトのAndroid JavaDocを見ると、それが何であるかを正確に説明しています。また、アクティビティクラスがコンテキストを拡張することにも注意してください。 –

+0

フラグメントコードで 'getActivity'を使うときは、すべてのアタッチ/デタッチイベントを正しく処理していることを確認してください。フラグメントがデタッチされたときに 'null 'にクラッシュするリソースへのアクセスのために、アプリケーションのコンテキストが十分であるため、フラグメント内にローカルに格納するのが少し妥当で、アクティビティから切り離されてもアクセスできます記憶されたコンテキストに頼るのではなく、現在のものを操作して、分離モードを正しく処理することです)。 – Ped7g

答えて

0

フラグメントの場合は、たとえばgetActivity()と呼ぶことができます。 onActivityCreatedコールバックの後に呼び出すことを確認してください。それ以外の場合は、nullが返されます。

0

Fragmentクラスからのリソースへのアクセスには2つの方法があります。 ユーティリティメソッドでは、Fragment#getContext()メソッドをパラメータとして呼び出す必要があります。

はあなたApplicationクラス以外Activityとすることができます非Fragmentクラスで今

public final class BaseContext { 

    private static Context CONTEXT; 

    public static void initialise(Context context) { 
     CONTEXT = context; 
    } 

    public static Context getContext() { 
     synchronized (BaseContext.class) { 
      if (BaseContext.CONTEXT == null) 
       throw new NullPointerException("Base Context not initialised. Call BaseContext.initialise()"); 
      else 
       return BaseContext.CONTEXT.getApplicationContext(); 
     } 
    } 
} 

でそれを初期化、BaseContextクラスを作成します。この方法により、リソースクラスに直接Fragment#getResources()

0

にアクセスできることに注意してください単純に呼び出してコンテキストを取得する

0

onCreateViewを使用すると、ルートビューを作成してサブオブジェクトのインスタンスを取得できます写真:

private ImageView difficultyContainerImageView1; 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_layout, container, false); 
     difficultyContainerImageView1 = (ImageView) rootView.findViewById(R.id.difficultyContainer); 
     return rootView; 
} 

onViewCreatedを使用してコンテンツを設定します。あなたは自由にonViewCreatedgetContextまたはgetActivityメソッドを呼び出すことができます。

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     difficultyContainerImageView1.setImageDrawable(
         new BitmapDrawable(getResources(), 
           ImgUtility.createRankingImg(getContext(), 3))); 

} 
-1

これは、その実装のAndroidシステムによって提供される抽象クラスであるコンテキスト ものです。それはだけでなく、アップの呼び出しなどなど、 を、活動を開始放送とインテントを受け取るなど、アプリケーションレベルの操作のためのコンテキストを取得するため

https://developer.android.com/reference/android/content/Context.html

で読み取り)、アプリケーション固有のリソースとクラスへのアクセスを可能にしますgetactivityメソッドを使用すると、アクティビティが返され、そこからコンテキストなどを取得できます。

Context context = getActivity().getApplicationContext() 
関連する問題