2012-02-03 68 views
1

我有下面的代码应该解析XML文档。活动调用抛出错误

我收到一个错误,指出'活动无法解决'。

如何让我的程序识别我的活动?

谢谢!

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) 
    { ... } 
+2

是的,它不编译吗?你不会传递参考到你的活动,但仍然像你一样引用它。 – L7ColWinters 2012-02-03 20:40:43

回答

1

你写它的方式,activity是一个未定义的局部变量。你如何预测这个工作?无论你的类需要接受活动的参数,并保存它,就像这样:

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

或者,如果不是在应用程序中相应的其他方式。