私は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アプリで表していますか?
getResources()ではなくgetContext()を使用します。 注意、getContext()はnullでもかまいません。 –
ContextオブジェクトのAndroid JavaDocを見ると、それが何であるかを正確に説明しています。また、アクティビティクラスがコンテキストを拡張することにも注意してください。 –
フラグメントコードで 'getActivity'を使うときは、すべてのアタッチ/デタッチイベントを正しく処理していることを確認してください。フラグメントがデタッチされたときに 'null 'にクラッシュするリソースへのアクセスのために、アプリケーションのコンテキストが十分であるため、フラグメント内にローカルに格納するのが少し妥当で、アクティビティから切り離されてもアクセスできます記憶されたコンテキストに頼るのではなく、現在のものを操作して、分離モードを正しく処理することです)。 – Ped7g