我已经创建了Project_info类并在Appfunc.class中创建了它的实例(静态公共Project_info m_Project),它扩展了Application。当我在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;
}
XMLparser类
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中使用此实例
答案 0 :(得分: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;
}
}
并且不要忘记在Manifest中添加它:
android:name="package.AppFuncs"
答案 1 :(得分: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;
}