2

EDIT: woah ... somehow i replaced this question with another one i was asking, glad there is this rollback feature

this specific question deals with the getter from my previous question

public class Impacts extends Activity implements View.OnClickListener
{
    boolean[] impactsb = new boolean[] {false, false, false, false, false, false, false, false}-
    public void onCreate (Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState)
...

    String getImpacts ()
    {
    String[] impactsn = new String[length];
    Resources myResources = this.getResources();
    impactsn = myResources.getStringArray(R.array.impacts);
    StringBuilder impactss = new StringBuilder();
    for (int i = 0; i < length; i ++)
        {
        if (impactsb[i])
            impactss.append(impactsn[i] + " | ");
        }
    if (String.valueOf(impactss) != "")
        impactss.insert(0, "Impacts: ");
    return String.valueOf(impactss);
    }

with these errors:

Impacts(ContextWrapper).getResources() line: 80
Impacts.getImpacts() line: 78

the final bracket of the below code:

@Override
public Resources getResources()
{
    return mBase.getResources();
}

and this line of code respectively:

impactsn = getResources().getStringArray(R.array.impacts);

here is my strings.xml (the relevent parts anyway)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="impacts">
        <item>GasOilChemical Pollutants</item>
        <item>Ghost Fishing</item>
        <item>Marsh Damage</item>
        <item>Navigational Hazard</item>
        <item>Shellfish Damage</item>
        <item>Waste Pollution</item>
        <item>Wildlife Entanglement</item>
        <item>Other</item>
    </string-array>
</resources>

i originally had the first item as:

<item>Gas/Oil/Chemical Pollutants</item>

but fixed that, hoping that would at least change the error if not fixing the problem. but nope, same error. any help would be vastly appreciated, im not terribly familiar with the use of array, especially getting resources for an array.

Logcat for exception:

06-05 23:02:30.792: ERROR/AndroidRuntime(3905): FATAL EXCEPTION: main
06-05 23:02:30.792: ERROR/AndroidRuntime(3905): java.lang.NullPointerException
06-05 23:02:30.792: ERROR/AndroidRuntime(3905): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
06-05 23:02:30.792: ERROR/AndroidRuntime(3905): at com.citsci.mardeb.Impacts.getImpacts(Impacts.java:79)
10
  • @hooraygradschool: Is the getImpacts() method part of an Activity or a helper class? Commented Jun 3, 2011 at 17:34
  • it is part of a class that extends the activity, i added that to the top in response to your comment. Commented Jun 3, 2011 at 17:41
  • @hooraygradschool: Ok, if it's a method of your Impacts Activity then why do you have an override for getResources()? Just using getResources() should work fine - try commenting out your overridden getResources() method. Commented Jun 3, 2011 at 17:48
  • oh no i see your confusion, the overide and that block go with the Impacts(ContextWrapper).getResources() line: 80 which is not part of my code, its part of the platform or source or whatever. sorry i didnt understand your questions/made an error in pasting the code to here. Commented Jun 3, 2011 at 18:23
  • @hooraygradschool: OK, I get it now - I can see that in the ContextWrapper source. Try separating your line of code into two in order to see what is causing the failure, i.e., Resources myResources = getResources(); impactsn = myResources.getStringArray(R.array.impacts); Commented Jun 3, 2011 at 18:31

3 Answers 3

4

i know this is poor coding, apologies in advance. so to fix this problem, i moved

String[] impactsn;
impactsn = getResources().getStringArray(R.array.impacts);

to my main activity and made it static. now i refer to it within my Impacts class as (MainActivity).impactsn[]

i know this is not how you are supposed to share objects between classes, but it seems to be the easiest way so far. this is yet another reason why i am regretting my decision to make all of my tabs seperate activities. if you have any suggestions or advice on how to fix this code without using static references or on reasons why all of my tabs should be the same activities, please chime in.

Sign up to request clarification or add additional context in comments.

Comments

1

suggest not to define an array in advance, and used directly without defining a variable

mAutoComplete.setAdapter (new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, getResources().getStringArray(R.array.OmskStreets)));

and more..

in Class

private String[] mStreets = null;

and in onCreate method

mStreets = getResources().getStringArray(R.array.OmskStreets);

Comments

0

Shouldn't define anything about UI before onCreate().

Moreover, the following is a poor coding example, but it works.

public void onCreate(Bundle icicle){
     super.onCreate(icicle);
...
...
...
     COUNTRIES  = getResources().getStringArray(R.array.COUNTRIES_ARRAY);

1 Comment

super.onCreate(bundle) was within the ellipses in my code. i have added it so it is apparent. im not sure what this answer adds to my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.