2012-06-02 33 views
1

我有個問題。 我想添加一個秒錶顯示到我的應用程序。我用這個page作爲參考。 我下載並添加了必要的commons-scxml-0.9.jar庫(帶源代碼)。 有在Eclipse中沒有錯誤,但是當我調試它,在這個class構造超一流(AbstractStateMachine)被稱爲用這個命令:秒錶構造函數需要URL scxmlDocument

超(StopWatch.class.getClassLoader()的getResource(「組織/阿帕奇/公地/ SCXML/ENV/stopwatch.xml「));

但超類構造函數沒有得到任何屬性。它預計最終URL scxmlDocument但只有null出現。

我知道stopwatch.xml看起來如何,但我應該在哪裏放置它,以及如何從它創建最終網址scxmlDocument

我嘗試了一切,但沒有任何工作。

謝謝大家!

這是stopwatch.xml,如果我將其添加日食因爲ID屬性報告錯誤:

`<?xml version="1.0" ?> 

  <transition event="watch.start" target="running" /> 
    </state> 
    <state id="running"> 
      <transition event="watch.split" target="paused" /> 
      <transition event="watch.stop" target="stopped" /> 
    </state> 
    <state id="paused"> 
      <transition event="watch.unsplit" target="running" /> 
      <transition event="watch.stop" target="stopped" /> 
    </state> 
    <state id="stopped"> 
      <transition event="watch.reset" target="reset" /> 
    </state> 

`

+0

是否有一個理由,爲什麼你正在經歷的這一切痛苦的,而不是隻使用'Chronometer'? – CommonsWare

+0

我先付款,但我會嘗試使用天文臺。感謝您的建議。 – m4gnum

回答

0

您的代碼看起來不錯。如果您在運行時遇到異常,則應該將其包含在您的問題中。除了你排除了前幾行和最後一行(這是故意的還是你的XML確實缺少這些行?)之外,XML看起來也很好。這是我的XML:

<?xml version="1.0"?> 
<!-- 
* Licensed to the Apache Software Foundation (ASF) under one or more 
* contributor license agreements. See the NOTICE file distributed with 
* this work for additional information regarding copyright ownership. 
* The ASF licenses this file to You under the Apache License, Version 2.0 
* (the "License"); you may not use this file except in compliance with 
* the License. You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
--> 
<scxml xmlns="http://www.w3.org/2005/07/scxml" 
     version="1.0" 
     initial="reset"> 

    <state id="reset"> 
     <transition event="watch.start" target="running"/> 
    </state> 

    <state id="running"> 
     <transition event="watch.split" target="paused"/> 
     <transition event="watch.stop" target="stopped"/> 
    </state> 

    <state id="paused"> 
     <transition event="watch.unsplit" target="running"/> 
     <transition event="watch.stop" target="stopped"/> 
    </state> 

    <state id="stopped"> 
     <transition event="watch.reset" target="reset"/> 
    </state> 

</scxml> 

至於在哪裏放置XML文件,通常你會把它放在SRC目錄中。當Eclipse執行構建時,它不知道如何處理XML文件,因此它的默認行爲是將其複製到編譯代碼所在的BIN目錄。您可能需要執行Project > Clean...才能實現此目的。這是運行時環境通常期望找到像這樣的資源的地方。

這是一個非常好的技巧:如果你在任何版本的Windows上進行開發,你可以到微軟的令人難以置信的好的Sysinternals站點(http://technet.microsoft.com/en-US/sysinternals)。在這裏您可以找到美妙的Process Monitor實用程序。下載,安裝並運行這個免費的實用程序。在該實用程序中,創建一個進程監視器篩選器:Path | contains | stopwatch.xml確保啓用此篩選器並禁用所有其他(預定義)篩選器。

然後運行你編譯好的秒錶程序。該實用程序將顯示應用程序(以及其中包含的所有類加載程序)的每個位置都會查找stopwatch.xml

然後,您可以移動/複製文件,以確保它位於應用程序查找文件的位置。這個實用程序對編寫J2EE應用程序(例如Tomcat,Websphere等)時發生的類似「我無法找到資源」的問題特別方便。

最後,當你沒有提及相關的jar文件什麼,確保這些都在類路徑:

commons-beanutils-1.9.2.jar 
commons-digester-2.1.jar 
commons-jexl-1.1.jar 
commons-logging-1.2.jar 
commons-scxml-0.9.jar 
log4j-1.2.17.jar 
xalan.jar 
相關問題