-3

我创建了一个构造函数类,我需要所有属性值,所以我试图解析构造函数类中的值,但我得到的是空值。所以请帮助我如何获得价值?我花更多时间在这个概念上

这是我的代码:

    public void test(NodeList panelNodes) {
        Region RegionOptions = new Region(this.getApplicationContext());
        RegionOptions.updateValues(RegionOptions.left);

        for (int i = 0; i < panelNodes.getLength()-1; i++) {
            Element panelElement = (Element) panelNodes.item(i);

            NamedNodeMap nod=panelElement.getAttributes();

            String layout=panelNodes.item(i).getNodeName();

            if(layout.compareTo("region") == 0) {
                for(int k=0;k<nod.getLength();k++){
                    Node nd=nod.item(k);
                    //System.out.println(layout+":"+ nd.getNodeName()+"::"+nd.getNodeValue());
                    if(nd.getNodeName().compareTo("left") == 0) {
                        RegionOptions.left = Integer.parseInt(nd.getNodeValue());

                    }else if(nd.getNodeName().compareTo("top") == 0) {
                        RegionOptions.top = Integer.parseInt(nd.getNodeValue());

                    }else if(nd.getNodeName().compareTo("height") == 0) {
                        RegionOptions.height = Integer.parseInt(nd.getNodeValue());
                    }else if(nd.getNodeName().compareTo("width") == 0) {
                        RegionOptions.width = Integer.parseInt(nd.getNodeValue());
                    }else if(nd.getNodeName().compareTo("userId") == 0) {
                        RegionOptions.userId = Integer.parseInt(nd.getNodeValue());
                    }else if(nd.getNodeName().compareTo("id") == 0) {
                        RegionOptions.id = nd.getNodeValue();
                    } else {
                        System.out.println("Unknown Tag:" + layout+":"+ nd.getNodeName()+"::"+nd.getNodeValue());
                    }


                }   
                //return(RegionOptions.userId, RegionOptions.bgcolor, RegionOptions.duration);

            }
            if(layout.compareTo("media") == 0) {
                for(int l=0;l<nod.getLength();l++){
                    Node nd=nod.item(l);
                    //System.out.println(layout+":"+ nd.getNodeName()+"::"+nd.getNodeValue());
                    if(nd.getNodeName().compareTo("type") == 0) {
                        RegionOptions.type = nd.getNodeValue();
                    }else if(nd.getNodeName().compareTo("duration") == 0) {
                        RegionOptions.duration = Integer.parseInt(nd.getNodeValue());
                    }else if(nd.getNodeName().compareTo("lkid") == 0) {
                        RegionOptions.lkid = Integer.parseInt(nd.getNodeValue());
                    }else if(nd.getNodeName().compareTo("userId") == 0) {
                        RegionOptions.userId = Integer.parseInt(nd.getNodeValue());
                    }else if(nd.getNodeName().compareTo("schemaVersion") == 0) {
                        RegionOptions.schemaVersion = Integer.parseInt(nd.getNodeValue());


                    } else {
                        System.out.println("Unknown Tag:" + layout+":"+ nd.getNodeName()+"::"+nd.getNodeValue());
                    }
                    }//return(RegionOptions.l, RegionOptions.bgcolor, RegionOptions.duration);

                }



        }

    }
}

构造函数类

我需要我的高度、顶部、宽度等...值

package com.example.xmlparsing;

import android.content.Context;

public class Region {
    int height; 
    int width;
    int top;
    int left;
    String id;
    int userId;
    String bgcolor;
    int schemaVersion;
    String type;
    int duration;
    int lkid;


    public Region(Context ApplicationContext) {
        //bgcolor= new String();
        // this.height= height;
    }

    public void updateValues(int left) {
        // TODO Auto-generated method stub
        this.left=left;
        //top=top;
System.out.println("value_left"+left);
    }
}
4

1 回答 1

1

你必须像这样设置你的构造函数:

    public Region(Context context, int height, int width){

      this.mContext = context:
      this.height = height;
      this.width = width;
   }
于 2012-12-21T12:16:51.710 回答