我在扩展应用程序的 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 中使用此实例