eeffoc
eeffoc

Reputation: 468

Obtaining stringArray from another Class

I'm assuming this is a really basic and a newb mistake but I am trying to get a StringArray for my ArrayAdapter from another class. I am even unsure if I have the correct idea, I am self taught beginner so any help is much appreciated.

My MainActivity.java snippet:

    Gold gold = new Gold();
    ArrayAdapter<String> ArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, gold.maxArray(highestLevel));
    ArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

And my Gold.java snippet:

public class Gold extends MainActivity{

String[] maxUpgradeLevel;

public String[] maxArray (int maxLevel) {
    if (maxLevel == 1){
        return maxUpgradeLevel = getResources().getStringArray(R.array.level1_entries_spinner);
    } else if (maxLevel == 2) {
        return maxUpgradeLevel = getResources().getStringArray(R.array.level2_entries_spinner);
    } else if (maxLevel == 3) {
        return maxUpgradeLevel = getResources().getStringArray(R.array.level3_entries_spinner);
    } else if (maxLevel == 4) {
        return maxUpgradeLevel = getResources().getStringArray(R.array.level4_entries_spinner);

and so on....

My logcat tells me:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

So I've been here for a few hours trying to tweak the code, the code was working fine before when I was using Integer[] but when I started with String[] I apparently can no longer get the resource.

Should I just put the content of Gold.java straight into MainActivity.java or is there a way of doing this that I am not aware of?

Markus

Upvotes: 0

Views: 53

Answers (2)

Harish Sridharan
Harish Sridharan

Reputation: 1090

Never create an instance for activity class. Activity instances should be created only through intents.

Fundamentally Gold gold = new Gold(); is wrong if Gold is extended class of MainActivity or any other activity.

Change your MainActivity call:

ArrayAdapter<String> ArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Gold.maxArray(getResources(),highestLevel));

and your Gold.java

    public class Gold {


        public static String[] maxArray (Resources res, int maxLevel) {
                if (maxLevel == 1){
                    return  res.getStringArray(R.array.level1_entries_spinner);
                } else if (maxLevel == 2) {
                    return  res.getStringArray(R.array.level2_entries_spinner);
                } else if (maxLevel == 3) {
                    return  res.getStringArray(R.array.level3_entries_spinner);
                } else if (maxLevel == 4) {
                    return  res.getStringArray(R.array.level4_entries_spinner);
                }
    //..
            }
        }
    }

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157437

you shouldn't use the new operator on classes that extends Activity. The Activity has to go through its lifecycle before you can use the Activity's context to access the resources. If you need to access maxArray from different classes and you don't want to replicate the code, you can make the method static, and pass a Context object as parameter.

public class Gold {


   public static String[] maxArray (Context context, int maxLevel) {
      if (maxLevel == 1){
         return context.getResources().getStringArray(R.array.level1_entries_spinner);
      }
      //

and you can reference it like Gold.maxArray(this, maxLevel) from onCreate of your Activity. It it is use just in one place, then you can move the code as private method of your Activity

Upvotes: 0

Related Questions