0

Ich bin ziemlich neu in Android und ich finde einige Schwierigkeiten, die folgende Sache im Zusammenhang mit dem Kontext zu tun.Wie erhalten Sie das Context-Objekt in ein klassenübergreifendes Fragment?

Also habe ich eine Utility-Klasse, die eine Hilfsmethode enthalten, erstellen und liefern einen Bitmap Bild, das ist der Code meiner Klasse:

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; 

    } 

} 

Wie Sie diese Klasse sehen kann, enthält die createRankingImg(), die das Context Objekt als Parameter nehmen und es verwenden, um ein Bild zu erstellen. Dieses Objekt wird verwendet, um ein Bild von den Ressourcen (in der BitmapFactory.decodeResource() Methode) abzurufen. Was genau repräsentiert das Context-Objekt in einer Android-Anwendung?

Ich weiß, dass der Kontext in eine Aktivitätsklasse I zu erhalten, die GetResources() Methode verwenden kann.

Mein Problem ist, dass ich den Kontext in eine Klasse erhalten muss, die Fragment ausgibt.

Ich habe so etwas wie dies:

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

Mein Problem ist, dass, wenn in der vorherigen Fragmente Klasse nenne ich die Methode:

ImgUtility.createRankingImg(getResources(), 3) 

es die GetResources() über Ausgang (dass ich dachte, geben Sie mir die Context, geben Sie mir die folgende Fehlermeldung:

Falscher 1. Argumenttyp. Gefunden: ‚android.content.res.Resources‘, erforderlich: ‚android.content.Context‘

So scheint es mir, dass in einer Klasse, die ein Fragment und kein Aktivität erstreckt die getResources() Methode zurückgeben Ressourcen Objekt stattdessen ein Kontext Objekt (wie in einer Aktivitätsklasse getan). Ist es wahr? Warum?

Wie kann ich den Kontext innerhalb einer Klasse erhalten, die Fragment erweitert? Und was genau repräsentiert den Kontext in einer Android App? Was vermisse ich? Wie kann ich dieses Problem beheben?

+2

'getActivity()' –

+0

Es sagt mir "Kann getActivity() nicht wiederherstellen?" Warum? – AndreaNobili

+1

'getActivity()' ist in 'Fragment' definiert, also sind Sie anscheinend nicht in einem' Fragment' oder es gibt einen Tippfehler oder so etwas. – laalto

Antwort

0

Erstellen Sie eine Anfangsfunktion, die Context als Parameter verwendet, damit Sie das Kontextobjekt aus der Aktivitätsklasse übergeben können.

+0

Fragmentkonstruktoren sollten keine Argumente annehmen. – laalto

Verwandte Themen