2015-11-19 62 views
4

我正在嘗試使用clojure創建一個簡單的android應用程序,該應用程序讀取csv文件並允許用戶對數據執行搜索等正則表達式。問題是,當我嘗試讀取數據時,出現以下異常。Clojure android無法讀取文件

IllegalArgumentException No implementation of method: :make-reader of protocol: #'clojure.java.io/IOFactory found for class: nil clojure.core/-cache-protocol-fn (core_deftype.clj:554)

我的研究說,這通常意味着Clojure是尋找的東西並不能找到它。但我無法弄清楚它可能是什麼或爲什麼。

這裏是拋出異常Clojure的代碼:

(ns org.stuff.events.data 
    (:require [clojure.java.io :as io])) 

(def data-file (io/file (io/resource "kamus.csv"))) 

(defn read-data [_] 
    (slurp data-file)) 

從最好的,我可以告訴「kamus.csv」是正確的目錄中,所以我不認爲這是它。如果我評估repl中的數據文件,我會得到nil

有人有任何想法來解決我的問題?


下面是其他項目文件僅供參考:

main.clj:

(ns org.stuff.events.main 
    (:require [neko.activity :refer [defactivity set-content-view!]] 
       [neko.debug :refer [*a]] 
       [neko.notify :refer [toast]] 
       [neko.ui :refer [config]] 
       [neko.resource :as res] 
       [neko.find-view :refer [find-view]] 
       [neko.threading :refer [on-ui]] 
       [org.stuff.events.data :as data] 
       [clojure.data.csv :as csv]) 
    (:import android.widget.TextView)) 

;; We execute this function to import all subclasses of R class. This gives us 
;; access to all application resources. 
(res/import-all) 



(def listing (atom "")) 


(defn get-elem [activity id] 
    (str (.getText (find-view activity id)))) 

(defn set-elem [activity id s] 
    (on-ui (config (find-view activity id) :text s))) 

(defn add-event [activity] 
    (swap! listing str (get-elem activity ::search-box) "\n") 
    (set-elem activity ::results @listing)) 


(defn main-layout [activity] 
    [:linear-layout {:orientation :vertical} 
    [:linear-layout {:orientation :horizontal     
        :layout-height :wrap} 
     [:edit-text {:id ::search-box 
         :hint "cari..." 
         :layout-width :fill}] 
     [:button {:text "Cari" 
        :on-click (fn [_] (add-event (*a)))}]] 
    [:text-view {:text @listing 
       :id ::results}]]) 


;; This is how an Activity is defined. We create one and specify its onCreate 
;; method. Inside we create a user interface that consists of an edit and a 
;; button. We also give set callback to the button. 
(defactivity org.stuff.events.MainActivity 
    :key :main 

    (onCreate [this bundle] 
    (.superOnCreate this bundle) 
    (neko.debug/keep-screen-on this) 
    (on-ui 
     (set-content-view! (*a) (main-layout (*a)))))) 

project.csj:

(defproject events/events "0.1.0-SNAPSHOT" 
    :description "FIXME: Android project description" 
    :url "http://example.com/FIXME" 
    :license {:name "Eclipse Public License" 
      :url "http://www.eclipse.org/legal/epl-v10.html"} 

    :global-vars {*warn-on-reflection* true} 

    :source-paths ["src/clojure" "src"] 
    :res-path "src/main/resources" 
    :java-source-paths ["src/java"] 
    :javac-options ["-target" "1.6" "-source" "1.6" "-Xlint:-options"] 
    :plugins [[lein-droid "0.4.3"]] 

    :dependencies [[org.clojure-android/clojure "1.7.0-r2"] 
       [neko/neko "4.0.0-alpha5"] 
       [org.clojure/data.csv "0.1.3"] ] 
    :profiles {:default [:dev] 

      :dev 
      [:android-common :android-user 
       {:dependencies [[org.clojure/tools.nrepl "0.2.10"]] 
       :target-path "target/debug" 
       :android {:aot :all-with-unused 
         :rename-manifest-package "org.stuff.events.debug" 
         :manifest-options {:app-name "EventsListing  (debug)"}}}] 
      :release 
      [:android-common 
       {:target-path "target/release" 
       :android 
       {;; :keystore-path "/home/user/.android/private.keystore" 
       ;; :key-alias "mykeyalias" 
       ;; :sigalg "MD5withRSA" 

       :ignore-log-priority [:debug :verbose] 
       :aot :all 
       :build-type :release}}]} 

    :android {;; Specify the path to the Android SDK directory. 
      :sdk-path "C:\\Users\\fhard\\AppData\\Local\\Android\\sdk" 

      ;; Try increasing this value if dexer fails with 
      ;; OutOfMemoryException. Set the value according to your 
      ;; available RAM. 
      :dex-opts ["-JXmx4096M" "--incremental"] 

      :target-version "15" 
      :aot-exclude-ns ["clojure.parallel" "clojure.core.reducers" 
         "cider.nrepl" "cider-nrepl.plugin" 
          "cider.nrepl.middleware.util.java.parser" 
          #"cljs-tooling\..+"]}) 

回答

0

從例外,它看起來像根據this answer,您的路徑有誤。當我嘗試用io/resource打開一個文件時,即使該文件實際存在,我也會得到同樣的錯誤。 io/reader可能更易於使用,因爲它會將輸入強制轉換爲閱讀器對象。另一方面,io /資源會返回一個URL,其路徑在jar中一次更改。 Check this.