SkyeBoniwell
SkyeBoniwell

Reputation: 7122

activity call throws error

I have the code below that is supposed to parse an XML document.

I'm getting an error stating that 'activity cannot be resolved'.

How can I get my program to recognize my activity?

Thanks!

package me.HelloAndroid;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Log;
import java.io.IOException;
import android.app.Activity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;

public class EncounterGenerator {

String encounterText;
int testint;
private String xmlValue;
private int encounterID;
public EncounterStats stats;

public EncounterGenerator() {
    Resources res = activity.getResources();
    XmlResourceParser xpp = res.getXml(R.xml.encounters);

    xpp.next();

    int eventType = xpp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT)
    { ... }

Upvotes: 1

Views: 56

Answers (1)

Brian Dupuis
Brian Dupuis

Reputation: 8176

The way you have it written, activity is an undefined local variable. How do you anticipate this working? Either your class needs to accept an Activity parameter and save it off, like so:

public EncounterGenerator(Activity activity) {
    Resources res = activity.getResources(); 
    ...
}

Or some other way if that isn't appropriate in your application.

Upvotes: 1

Related Questions