【问题标题】:How obtain the Context object into a class extenging Fragment?如何将 Context 对象获取到类扩展 Fragment 中?
【发布时间】:2016-12-27 15:52:35
【问题描述】:

我是 Android 的新手,我发现做以下与上下文相关的事情有些困难。

所以我有一个实用程序类,其中包含一个实用程序方法,用于创建和返回 Bitmap 图像,这是我的类的代码:

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() 方法)。什么是 Context 对象在 Android 应用程序中的确切代表?

我知道要将 context 获取到活动类中,我可以使用 getResources() 方法。

我的问题是我必须将上下文获取到一个扩展 Fragment 的类中。

我有这样的事情:

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() 输出传递给它(我认为给我 Context,IDE 给我以下错误消息:

第一个参数类型错误。找到:'android.content.res.Resources', 必需:'android.content.Context'

所以在我看来,getResources() 方法返回一个 Fragment 而不是 Activity 的类>Resources 对象而不是 Context 对象(就像在 Activity 类中所做的那样)。这是真的吗?为什么?

如何在扩展 Fragment 的类中获取上下文? Android 应用程序中的 Context 到底是什么?我错过了什么?我该如何解决这个问题?

【问题讨论】:

  • 它对我说“无法解决 getActivity()”?为什么?
  • getActivity() 是在 Fragment 中定义的,所以显然你不在 Fragment 中,或者有错别字之类的。

标签: java android android-fragments android-resources android-context


【解决方案1】:

创建一个以 Context 作为参数的初始函数,以便您可以从活动类传递上下文对象。

【讨论】:

  • 片段构造函数不应带参数。
猜你喜欢
  • 2020-05-15
  • 1970-01-01
  • 2018-09-09
  • 1970-01-01
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 2020-12-12
  • 1970-01-01
相关资源
最近更新 更多