user2598157
user2598157

Reputation:

Global variable is not accessible

I have made class of Project_info and make its instance (static public Project_info m_Project) in Appfunc.class which extends Application. When i access this instance in XMLparserclass then its working fine but when i access same instance in another activity then it is not accessible.

My Appfunc class

public class AppFuncs extends Application
{
    public static Project_info m_Project;

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

}

My Project_info class

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 class

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());

}

But i am unable to access m_project object in any other activity. What i want is that m_project instance gets all of its attribute information from XMl file in XMLparser.Java and then this instance is used in Map.class Activity

Upvotes: 0

Views: 109

Answers (2)

Srikanth Roopa
Srikanth Roopa

Reputation: 1790

Try this:

     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;
    }    
}

and don't forgot to add this in Manifest:

android:name="package.AppFuncs"

Upvotes: 1

Brtle
Brtle

Reputation: 2297

You can try to define public attributes in your Project_info class:

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;

}

Upvotes: 0

Related Questions