0

I am dynamically creating objects in the program and populating them from an array.xml. In the array.xml I have a series of tools and values and I need to load these into the class values for each item.

Here is what I have in the class;

public class ToolImporter extends Application{

public static Tool[] tools;
private String[] aTool;
private int i;

public ToolImporter() {

    aTool = getResources().getStringArray(R.array.tools); //null pointer?

    // TODO Auto-generated constructor stub
}

and this is my array.xml;

    <array name="tools">
        <item name="SAW">
            <id>1</id>
            <image>R.drawable.image_saw100x60px</image>
            <boxX>100</boxX>
            <boxY>100</boxY>
            <worktopX>200</worktopX>
            <worktopY>200</worktopY>
        </item>
        <item name="SCREWDRIVER">
            <id>2</id>
            <image>R.drawable.image_screwdriver100x60px</image>
            <boxX>150</boxX>
            <boxY>100</boxY>
            <worktopX>250</worktopX>
            <worktopY>200</worktopY>
        </item>
        <item name="HAMMER">
            <id>3</id>
            <image>R.drawable.image_hammer100x60px</image>
            <boxX>200</boxX>
            <boxY>100</boxY>
            <worktopX>300</worktopX>
            <worktopY>200</worktopY>
        </item>
    </array>

However, it throws a null pointer on the "//null pointer?" line. Can anyone offer advice on what i'm doing wrong to import it?

3 Answers 3

1

According to this post:

You shouldn't call getResources() unless onCreate() callback has been triggered.

public class StackStackActivity extends Activity 
{

    String[] myArray = getResources().getStringArray(R.array.glenns); // This returns null

    public StackStackActivity()
    {

        myArray = getResources().getStringArray(R.array.glenns); // This returns null also
    }

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        myArray = getResources().getStringArray(R.array.glenns); // Returns an array from resources
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

look at question he is trying to access Resources inside Application class instead of Activity
My statement still stands.
I will attempt to import the array in the activity and pass it through to the importer class
@Intern87 it should work the same as an Application, the key is using the onCreate method to do it.
1

Create field variables in your Application class and then initialize them inside the onCreate method within your main activity class.

Comments

0

Make a 'Context' field in ToolImporter class. Pass context from your activity to ToolImporter class in ToolImporter constructor.

Use context field to access getResources()

aTool = context.getResources().getStringArray(R.array.tools); 

Comments

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.