2012-11-17 90 views
0

我一直在使用簡單的嘗試在我的XML文件中這一類閱讀。我真的不知道我是否正確註釋了類。Android的XML註釋

我不知道如果我需要這部分:

public Frame() 
{ 
    super(); 
} 

public Frame(int num, int x, int y, int width, int height,int offsetx,int offsety, int duration) 
{ 
    this.Num = num; 
    this.X = x; 
    this.Y = y; 
    this.Width = width; 
    this.Height = height; 
    this.OffsetX = offsetx; 
    this.OffsetY = offsety; 
    this.Duration = duration;] 

什麼是超()呢?我需要獲得者/安裝者嗎?我添加了getters或setters嗎?他們是自動自動還是自動調用?

下面是完整的類:

public class SpriteAnimationManag 
{ 
// Animation frame class 



@Element(name = "Frame") 
public class Frame 
{ 

    @Element(name = "Num") 
    public int Num; 


    @Element(name = "X") 
    public int X; 

      @Element(name = "Y") 
    public int Y; 


    @Element(name = "Width") 
    public int Width; 


    @Element(name = "Height") 
    public int Height; 


    @Element(name = "OffSetX") 
    public int OffsetX; 


    @Element(name = "OffSetY") 
    public int OffsetY; 


    @Element(name = "Duration") 
    public float Duration; 

    public Frame() 
    { 
     super(); 
    } 

    public Frame(int num, int x, int y, int width, int height,int offsetx,int offsety, int duration) 
    { 
     this.Num = num; 
     this.X = x; 
     this.Y = y; 
     this.Width = width; 
     this.Height = height; 
     this.OffsetX = offsetx; 
     this.OffsetY = offsety; 
     this.Duration = duration; 
    } 



} 

// Animaiton class to hold the name and frames 
public class Animation 
{ 

    @Element(name = "Name") 
    public String Name; 


    @Element(name = "FrameRate") 
    public int FrameRate;//may need elementarray or list??? 

    @Element(name = "Loop") 
    public boolean Loop; 

    @Element(name = "Pingpong") 
    public boolean Pingpong; 


    @ElementArray(name = "Frames") 
    public Frame[] Frames; 

public Animation() 
{ 
    super(); 
} 

public Animation(String name, int framerate, boolean loop, boolean pingpong, Frame[] frames) 
{ 
this.Name = name; 
this.FrameRate = framerate; 
this.Loop = loop; 
this.Pingpong = pingpong; 
this.Frames = frames; 
} 


} 

// The Sprite Texture stores the Sprite Sheet path.fr 
public class SpriteTexture 
{ 
    // The Sprite Sheet texture file path 

    @Element(name = "path") 
    public String Path; 

    public SpriteTexture() 
    { 
     super(); 
    } 

    public SpriteTexture(String path) 
    { 
     this.Path = path; 
    } 

} 

// Aniamtion Set contains the Sprite Texture and Animaitons. 

@Root(name = "Animations") 
public static class XNAAnimationSet 
{ 
    // The sprite texture object 

    @Element(name = "Texture") 
    public SpriteTexture SpriteTexture; 

    // The animation array in the Animation Set 
     @ElementArray(name = "Animation") 
    public Animation[] Animations; 

    public XNAAnimationSet() 
    { 
     super(); 
    } 

    public XNAAnimationSet(SpriteTexture spritetexture, Animation[] animations) 
    { 
     this.SpriteTexture = spritetexture; 
     this.Animations = animations; 
    } 
} 

// Sprite Animation Manager class 
public final static class SpriteAnimationManager 
{ 
    private static final String XNAAnimationSet = null;//was static private static 
    public static int AnimationCount; 

    // Read the Sprite Sheet Description information from the description xml file 
    public static XNAAnimationSet Read(String filename) throws Exception 
    { 

     XNAAnimationSet animationSet = new XNAAnimationSet(); 


      Serializer serializer = new Persister(); 

      try { 
       animationSet = serializer.read(XNAAnimationSet.class, filename); 

      } 
      catch (Exception e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


     // Count the animations to Animation Count 
     AnimationCount = animationSet.Animations.length; 

     return animationSet; 
    } 
    } 
} 

我一直想看看有什麼正在試圖將類寫入文件中讀取。該文件已創建,但它是空的。

誰能告訴我,如果我正確註解嗎?我究竟做錯了什麼?

+0

'超()'調用超類的構造函數。這是基本的Java,所以我想你在嘗試更復雜的事情之前首先需要刷新一些Java技能。 – Veger

+0

我同意@Veger,你所有的POJO類都是SpriteAnimationManag的內部類,這不會工作。它們應該是單獨的類或至少是靜態的內部類(膨脹的syntaxic選擇)。 – Snicolas

+0

我知道Java和C#很像。該代碼在C#中爲什麼不在Java中工作?你想看到我的C#代碼以供參考嗎? 我學習編程的方式就是在做。此外,我只是一個業餘愛好者,我喜歡學習新事物。我沒有時間去「學習」,我只有時間去做。 –

回答

1

我用JAXB存在的最後一天來解析我的XML,我不知道它是怎樣的方式similiar你這樣做,但生病提及幾個我所需要的東西:

首先,我想我需要在我的課,這對你也只是一個無參數的構造 -

public Frame(){}; 

我相信你確實需要干將,你有什麼存在的arent getter/setter方法,您只需聲明變量,這真的是基本的Java的東西,所以它可能是值得在這之前,你繼續閱讀。 當你正確定義你的干將,你再把@XMLElement上面標註每個人,而不是你上面的變量聲明符。 吸氣的樣子:

@XMLElement  
public string getName(){ return this.Name}; 

而且ID建議嘗試一次解析一個類時,你有多個內部類在這裏,我想想象得到,當你試圖解析凌亂,我認爲你需要有@RootElement以上的類名稱聲明,所以XML知道你創建什麼類型的對象。

無論如何,有幾件事情把我的頭頂部,好運吧!

+0

謝謝,但使用Simple允許我以這種方式進行註釋,至少這是所有教程的作用。我會盡力做的getter setter方法(我以爲我有吸氣部分)和BTW我有一個根元素 –

+0

任何人想幫助給予不利於在所有的答案,也沒有任何相關性 –