2012-03-27 84 views
0

我在使用CssResource的同時收到奇怪的NPE。 我已經定義了以下在我的CSS文件中的兩個CSS類:GWT CssResource拋出NPE

.mainPanelStyle 
{ 
    height: 250px; 
    width: 250px; 
    background-color: gray; 
    cursor: pointer; 
} 

.titleLabelStyle 
{ 
    font-size: 14px; 
    font-family: Segoe UI Semibold; 
    color: white; 
} 

然後,我定義的接口爲:

public interface HomePanelCssResource 
     extends CssResource 
    { 
     String mainPanelStyle(); 

     String titleLabelStyle(); 
    } 

,並增加了進入ClientBundle類爲:

@Source("com/myapp/homePanel/css/homePanelDefault.css") 
    HomePanelCssResource getHomePanelStyle(); 

當我在類文件中使用這個CSS資源時,調用cssResource.mainPanelStyle()工作正常。但打電話cssResource.titleLabelStyle()投擲NPE。它說:「你忘了添加必需的依賴?」。

我不知道什麼是錯的。你能幫我解決這個問題嗎?

+0

你不應該把身邊 「的Segoe UI Semibold」 行情?我無法解釋NPE(不是沒有堆棧跟蹤),但這可能是原因。 – 2012-03-27 10:14:48

+0

而不是'cssResource.titleLabelStyle()'你的意思是'getHomePanelStyle.titleLabelStyle()'?也許你沒有實例化你的資源實例。 – checketts 2012-03-29 04:17:34

回答

3

我不知道你的錯誤來自,但是這可能會幫助你......

這是我如何定義和訪問的CssResource:

我的主類: 它定義客戶端軟件包和Cssresource名稱。

interface MyClientBundler extends ClientBundle { 

    public static final MyClientBundler INSTANCE = GWT.create(MyClientBundler.class); 

    //my.css and the whole packege path are allowed 
    @Source("stefank/client/my.css") 
    public MyResources css(); 
} 

interface MyResources extends CssResource { 
    String test(); 
} 


public void onModuleLoad() { 
    Label l = new Label("Resource Test"); 

    MyClientBundler.INSTANCE.css().ensureInjected(); //importaten, else the resource might not be loaded jet 

    l.addStyleName(MyClientBundler.INSTANCE.css().test()); 

    RootPanel.get().add(l); 
} 

我的CSS種源(INT相同packagage作爲主類):

.test { 
    background-color: blue; 
    height: 50px; 
    width: 100px; 
} 

這給我的標籤大小合適和正確的背景顏色。

鏈接: CssResource

+0

斯蒂芬,你搖滾!感謝ensureInjected()!!這讓我瘋狂。 – Chania 2012-09-11 15:16:39

+0

:)謝謝,我也花了很長時間^^ – Stefan 2012-09-11 15:25:05