0

我在扩展应用程序的 Appfunc.class 中创建了 Project_info 类并创建它的实例(静态公共 Project_info m_Project)。当我在 XMLparserclass 中访问此实例时,它工作正常,但是当我在另一个活动中访问同一实例时,它无法访问。

我的 Appfunc 类

public class AppFuncs extends Application
{
    public static Project_info m_Project;

    public AppFuncs()
    {
                m_Project=new Project_info();
    }

}

我的 Project_info 课程

public class Project_info {
    ArrayList<Layer> newlayer=new ArrayList<Layer>();

    float m_Xmin;
    float m_Ymin;
    float m_Xmax;
    float m_Ymax;
    String m_ExtendBound;
    int m_ProjectID;
    String m_ProjectName;
    String m_Description;
    String m_StartDate;
    String m_Ownership;
    String m_LastModified;
    String m_Datum;
    String m_Projection;
    int m_NoSignificantDecimals;
    int m_ZoomCurrent;
    int m_RasterHeight;
    int m_Background;

}

XML解析器类

public class XMLparser extends Activity {

    String OGLpath="/sdcard/OGLDataTest/Feedlot";
    File file= new File(OGLpath);
    File[] files=file.listFiles();

    private String name;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getIntent().getExtras()!=null)
        {
            if (getIntent().getExtras().containsKey(FileChooser.Gis_path))
            {
                name=getIntent().getExtras().getString(FileChooser.Gis_path);

            }

        }
        Document doc=null;
        try{
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            //doc = dBuilder.parse(assManager.open("e.xml"));
            File f= new File(name);
            doc = dBuilder.parse((f));
        }
        catch (Exception e) {
            e.printStackTrace();
        }


        AppFuncs f=(AppFuncs) getApplicationContext();

        // Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList pjj=doc.getElementsByTagName("ProjectInformation");
        NodeList pj=pjj.item(0).getChildNodes();
        //NodeList pj=child.item(0).getChildNodes();

        for (int p=0;p<pj.getLength();p++)
        {
            if (pj.item(p).getNodeName().equals("Details"))
            {
                Element attribute=(Element)pj.item(p);
                if(attribute.hasAttribute("ProjectID"))
                    f.m_Project.m_ProjectID=Integer.parseInt(((Element) pj.item(p)).getAttributes().getNamedItem("ProjectID").getNodeValue());

                if(attribute.hasAttribute("ProjectName"))
                    f.m_Project.m_ProjectName=(((Element) pj.item(p)).getAttributes().getNamedItem("ProjectName").getNodeValue());

                if(attribute.hasAttribute("Description"))
                    f.m_Project.m_Description=(((Element) pj.item(p)).getAttributes().getNamedItem("Description").getNodeValue());

                if(attribute.hasAttribute("StartDate"))
                    f.m_Project.m_StartDate=(((Element) pj.item(p)).getAttributes().getNamedItem("StartDate").getNodeValue());

                if(attribute.hasAttribute("OwnerShip"))
                    f.m_Project.m_Ownership=(((Element) pj.item(p)).getAttributes().getNamedItem("OwnerShip").getNodeValue());

                if(attribute.hasAttribute("LastModified"))
                    f.m_Project.m_LastModified=(((Element) pj.item(p)).getAttributes().getNamedItem("LastModified").getNodeValue());

}

但我无法在任何其他活动中访问 m_project 对象。我想要的是 m_project 实例从 XMLparser.Java 中的 XMl 文件中获取其所有属性信息,然后在 Map.class Activity 中使用此实例

4

2 回答 2

1

试试这个:

     public class AppFuncs extends Application
   {
    public static Project_info m_Project;

    public void onCreate() {
        super.onCreate();
          m_Project=new Project_info();
    }

    public static Project_info getInstance() {
        return m_Project;
    }    
}

并且不要忘记在清单中添加这个:

android:name="package.AppFuncs"
于 2013-07-29T12:37:07.847 回答
0

您可以尝试在Project_info类中定义公共属性:

public class Project_info {
    public ArrayList<Layer> newlayer=new ArrayList<Layer>();

    public float m_Xmin;
    public float m_Ymin;
    public float m_Xmax;
    public float m_Ymax;
    public String m_ExtendBound;
    public int m_ProjectID;
    public String m_ProjectName;
    public String m_Description;
    public String m_StartDate;
    public String m_Ownership;
    public String m_LastModified;
    public String m_Datum;
    public String m_Projection;
    public int m_NoSignificantDecimals;
    public int m_ZoomCurrent;
    public int m_RasterHeight;
    public int m_Background;

}
于 2013-07-29T12:27:05.537 回答