Error with getResources() in a simple java class [duplicate]












-1
















This question already has an answer here:




  • Using getResources() in non-activity class

    10 answers




I've got a problem with a simple tutorial code i've follow to learn Recyclerview.
First I create a class to represent item like :



package tom_d.fr.testrecyclerview;

import android.content.res.Resources;
import java.util.ArrayList;

public class Planete {

public String mNom;
public int mDistance;
protected ArrayList<Planete> mListe;

Planete(String nom, int distance) {
mNom = nom; // nom de la planète
mDistance = distance; // distance au soleil en Gm

Resources res = getResources();
final String noms = res.getStringArray(R.array.noms);
final int distances = res.getIntArray(R.array.distances);

mListe = new ArrayList<Planete>();
for (int i=0; i<noms.length; ++i) {
mListe.add(new Planete(noms[i], distances[i]));
}
}


I get error: cannot find symbol method getResources()



Even if I try with :



Resources res = getApplicationContext().getResources();


How to proceed ? I just want to acess to an xml file with arrays :(



I've also search for this error here on stackoverflow and other website but no functionnal answer....



I think I've made a mistake but I don't know where :/










share|improve this question













marked as duplicate by Nilesh Rathod android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 3:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    -1
















    This question already has an answer here:




    • Using getResources() in non-activity class

      10 answers




    I've got a problem with a simple tutorial code i've follow to learn Recyclerview.
    First I create a class to represent item like :



    package tom_d.fr.testrecyclerview;

    import android.content.res.Resources;
    import java.util.ArrayList;

    public class Planete {

    public String mNom;
    public int mDistance;
    protected ArrayList<Planete> mListe;

    Planete(String nom, int distance) {
    mNom = nom; // nom de la planète
    mDistance = distance; // distance au soleil en Gm

    Resources res = getResources();
    final String noms = res.getStringArray(R.array.noms);
    final int distances = res.getIntArray(R.array.distances);

    mListe = new ArrayList<Planete>();
    for (int i=0; i<noms.length; ++i) {
    mListe.add(new Planete(noms[i], distances[i]));
    }
    }


    I get error: cannot find symbol method getResources()



    Even if I try with :



    Resources res = getApplicationContext().getResources();


    How to proceed ? I just want to acess to an xml file with arrays :(



    I've also search for this error here on stackoverflow and other website but no functionnal answer....



    I think I've made a mistake but I don't know where :/










    share|improve this question













    marked as duplicate by Nilesh Rathod android
    Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 22 '18 at 3:56


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      -1












      -1








      -1









      This question already has an answer here:




      • Using getResources() in non-activity class

        10 answers




      I've got a problem with a simple tutorial code i've follow to learn Recyclerview.
      First I create a class to represent item like :



      package tom_d.fr.testrecyclerview;

      import android.content.res.Resources;
      import java.util.ArrayList;

      public class Planete {

      public String mNom;
      public int mDistance;
      protected ArrayList<Planete> mListe;

      Planete(String nom, int distance) {
      mNom = nom; // nom de la planète
      mDistance = distance; // distance au soleil en Gm

      Resources res = getResources();
      final String noms = res.getStringArray(R.array.noms);
      final int distances = res.getIntArray(R.array.distances);

      mListe = new ArrayList<Planete>();
      for (int i=0; i<noms.length; ++i) {
      mListe.add(new Planete(noms[i], distances[i]));
      }
      }


      I get error: cannot find symbol method getResources()



      Even if I try with :



      Resources res = getApplicationContext().getResources();


      How to proceed ? I just want to acess to an xml file with arrays :(



      I've also search for this error here on stackoverflow and other website but no functionnal answer....



      I think I've made a mistake but I don't know where :/










      share|improve this question















      This question already has an answer here:




      • Using getResources() in non-activity class

        10 answers




      I've got a problem with a simple tutorial code i've follow to learn Recyclerview.
      First I create a class to represent item like :



      package tom_d.fr.testrecyclerview;

      import android.content.res.Resources;
      import java.util.ArrayList;

      public class Planete {

      public String mNom;
      public int mDistance;
      protected ArrayList<Planete> mListe;

      Planete(String nom, int distance) {
      mNom = nom; // nom de la planète
      mDistance = distance; // distance au soleil en Gm

      Resources res = getResources();
      final String noms = res.getStringArray(R.array.noms);
      final int distances = res.getIntArray(R.array.distances);

      mListe = new ArrayList<Planete>();
      for (int i=0; i<noms.length; ++i) {
      mListe.add(new Planete(noms[i], distances[i]));
      }
      }


      I get error: cannot find symbol method getResources()



      Even if I try with :



      Resources res = getApplicationContext().getResources();


      How to proceed ? I just want to acess to an xml file with arrays :(



      I've also search for this error here on stackoverflow and other website but no functionnal answer....



      I think I've made a mistake but I don't know where :/





      This question already has an answer here:




      • Using getResources() in non-activity class

        10 answers








      java android






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 2:39









      Tom DetranchantTom Detranchant

      12




      12




      marked as duplicate by Nilesh Rathod android
      Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 22 '18 at 3:56


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Nilesh Rathod android
      Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 22 '18 at 3:56


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          3 Answers
          3






          active

          oldest

          votes


















          2














          You need a context to access resource. Pass a context from your activity to your class



          Planete(Context context, String nom, int distance) {
          Resources res = context.getResources();
          }


          Then in your activity



          new Planete(this, "sample", 1);





          share|improve this answer































            0














            transform your class like this



            public class Planete {

            private Context context;
            public String mNom;
            public int mDistance;
            protected ArrayList<Planete> mListe;

            Planete(Context context, String nom, int distance) {
            mNom = nom; // nom de la planète
            mDistance = distance; // distance au soleil en Gm

            Resources res = context.getResources();
            final String noms = res.getStringArray(R.array.noms);
            final int distances = res.getIntArray(R.array.distances);

            mListe = new ArrayList<Planete>();
            for (int i=0; i<noms.length; ++i) {
            mListe.add(new Planete(noms[i], distances[i]));
            }
            }
            }


            this will work






            share|improve this answer































              0














              You can not use "resources" directly,
              Because this is the external class file. And where this external class file is used, it is not known.



              If you use getAplicationContext() then it still does not allow access to resources.



              Therefore you can use project resources with context to those activity where it calls or uses.



              Planete(Context context, String nom, int distance) {
              Resources res = context.getResources();
              }





              share|improve this answer






























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                2














                You need a context to access resource. Pass a context from your activity to your class



                Planete(Context context, String nom, int distance) {
                Resources res = context.getResources();
                }


                Then in your activity



                new Planete(this, "sample", 1);





                share|improve this answer




























                  2














                  You need a context to access resource. Pass a context from your activity to your class



                  Planete(Context context, String nom, int distance) {
                  Resources res = context.getResources();
                  }


                  Then in your activity



                  new Planete(this, "sample", 1);





                  share|improve this answer


























                    2












                    2








                    2







                    You need a context to access resource. Pass a context from your activity to your class



                    Planete(Context context, String nom, int distance) {
                    Resources res = context.getResources();
                    }


                    Then in your activity



                    new Planete(this, "sample", 1);





                    share|improve this answer













                    You need a context to access resource. Pass a context from your activity to your class



                    Planete(Context context, String nom, int distance) {
                    Resources res = context.getResources();
                    }


                    Then in your activity



                    new Planete(this, "sample", 1);






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 22 '18 at 2:45









                    Android_K.DoeAndroid_K.Doe

                    655119




                    655119

























                        0














                        transform your class like this



                        public class Planete {

                        private Context context;
                        public String mNom;
                        public int mDistance;
                        protected ArrayList<Planete> mListe;

                        Planete(Context context, String nom, int distance) {
                        mNom = nom; // nom de la planète
                        mDistance = distance; // distance au soleil en Gm

                        Resources res = context.getResources();
                        final String noms = res.getStringArray(R.array.noms);
                        final int distances = res.getIntArray(R.array.distances);

                        mListe = new ArrayList<Planete>();
                        for (int i=0; i<noms.length; ++i) {
                        mListe.add(new Planete(noms[i], distances[i]));
                        }
                        }
                        }


                        this will work






                        share|improve this answer




























                          0














                          transform your class like this



                          public class Planete {

                          private Context context;
                          public String mNom;
                          public int mDistance;
                          protected ArrayList<Planete> mListe;

                          Planete(Context context, String nom, int distance) {
                          mNom = nom; // nom de la planète
                          mDistance = distance; // distance au soleil en Gm

                          Resources res = context.getResources();
                          final String noms = res.getStringArray(R.array.noms);
                          final int distances = res.getIntArray(R.array.distances);

                          mListe = new ArrayList<Planete>();
                          for (int i=0; i<noms.length; ++i) {
                          mListe.add(new Planete(noms[i], distances[i]));
                          }
                          }
                          }


                          this will work






                          share|improve this answer


























                            0












                            0








                            0







                            transform your class like this



                            public class Planete {

                            private Context context;
                            public String mNom;
                            public int mDistance;
                            protected ArrayList<Planete> mListe;

                            Planete(Context context, String nom, int distance) {
                            mNom = nom; // nom de la planète
                            mDistance = distance; // distance au soleil en Gm

                            Resources res = context.getResources();
                            final String noms = res.getStringArray(R.array.noms);
                            final int distances = res.getIntArray(R.array.distances);

                            mListe = new ArrayList<Planete>();
                            for (int i=0; i<noms.length; ++i) {
                            mListe.add(new Planete(noms[i], distances[i]));
                            }
                            }
                            }


                            this will work






                            share|improve this answer













                            transform your class like this



                            public class Planete {

                            private Context context;
                            public String mNom;
                            public int mDistance;
                            protected ArrayList<Planete> mListe;

                            Planete(Context context, String nom, int distance) {
                            mNom = nom; // nom de la planète
                            mDistance = distance; // distance au soleil en Gm

                            Resources res = context.getResources();
                            final String noms = res.getStringArray(R.array.noms);
                            final int distances = res.getIntArray(R.array.distances);

                            mListe = new ArrayList<Planete>();
                            for (int i=0; i<noms.length; ++i) {
                            mListe.add(new Planete(noms[i], distances[i]));
                            }
                            }
                            }


                            this will work







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 22 '18 at 2:46









                            Har KalHar Kal

                            612414




                            612414























                                0














                                You can not use "resources" directly,
                                Because this is the external class file. And where this external class file is used, it is not known.



                                If you use getAplicationContext() then it still does not allow access to resources.



                                Therefore you can use project resources with context to those activity where it calls or uses.



                                Planete(Context context, String nom, int distance) {
                                Resources res = context.getResources();
                                }





                                share|improve this answer




























                                  0














                                  You can not use "resources" directly,
                                  Because this is the external class file. And where this external class file is used, it is not known.



                                  If you use getAplicationContext() then it still does not allow access to resources.



                                  Therefore you can use project resources with context to those activity where it calls or uses.



                                  Planete(Context context, String nom, int distance) {
                                  Resources res = context.getResources();
                                  }





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    You can not use "resources" directly,
                                    Because this is the external class file. And where this external class file is used, it is not known.



                                    If you use getAplicationContext() then it still does not allow access to resources.



                                    Therefore you can use project resources with context to those activity where it calls or uses.



                                    Planete(Context context, String nom, int distance) {
                                    Resources res = context.getResources();
                                    }





                                    share|improve this answer













                                    You can not use "resources" directly,
                                    Because this is the external class file. And where this external class file is used, it is not known.



                                    If you use getAplicationContext() then it still does not allow access to resources.



                                    Therefore you can use project resources with context to those activity where it calls or uses.



                                    Planete(Context context, String nom, int distance) {
                                    Resources res = context.getResources();
                                    }






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 22 '18 at 3:08









                                    D PrinceD Prince

                                    484417




                                    484417















                                        Popular posts from this blog

                                        If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                                        Alcedinidae

                                        Can an atomic nucleus contain both particles and antiparticles? [duplicate]