Error with getResources() in a simple java class [duplicate]
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 :/
java android
marked as duplicate by Nilesh Rathod
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.
add a comment |
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 :/
java android
marked as duplicate by Nilesh Rathod
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.
add a comment |
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 :/
java android
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
java android
asked Nov 22 '18 at 2:39
Tom DetranchantTom Detranchant
12
12
marked as duplicate by Nilesh Rathod
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
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.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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);
add a comment |
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
add a comment |
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();
}
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 22 '18 at 2:45
Android_K.DoeAndroid_K.Doe
655119
655119
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 22 '18 at 2:46
Har KalHar Kal
612414
612414
add a comment |
add a comment |
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();
}
add a comment |
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();
}
add a comment |
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();
}
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();
}
answered Nov 22 '18 at 3:08
D PrinceD Prince
484417
484417
add a comment |
add a comment |