【问题标题】:libGDX Cannot read file from Gdx.files.internallibGDX 无法从 Gdx.files.internal 读取文件
【发布时间】:2018-02-13 16:16:52
【问题描述】:

我的 libGDX 项目有点问题。在课堂上(高中,不要想太多),我们要做一个刽子手游戏。和我的一些朋友一起,我们开始使用 Scala。嗯,它有效,有点。 但问题是我无法读取生成单词的文件。这是一个 yaml 文件(words.yml),位于 android 子项目的资产中。它被打包在 .jar 文件中,我检查了它,但是当我尝试使用 Gdx.files.internal 读取它时,它会以 NullPointerException 停止。这让我很烦,因为我能够读取图像文件。我认为使用 Gdx.files.internal 从核心项目的主文件中读取是不可能的。但情况似乎并非如此,因为如果我把它放在那个文件中,它也会崩溃。

它消亡的领域:

private val map: (Int, Array[String]) = {
    val yaml: Yaml = new Yaml //using the snakeyaml library

    //Debug
    println("DBG")
    println(Gdx.files.local("words.yml").path()) //<-------It crashes here
    println("more DBG")

    val tmp: util.LinkedHashMap[String, Any] = yaml.load(
        scala.io.Source.fromFile(Gdx.files.internal("words.yml").file()) mkString) 
       //^^^^^or here if I remove the debug part^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    (tmp.get("wordCount").asInstanceOf[Int],
    tmp.get("words").toString.replaceAll("[\\[\\]]","").split(",")
        .map((s: String) => {
            if(s(0) == ' ') s.substring(1)
            else s
        }))
}

错误信息:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.infosoftcorp.wordpress.ish.Save.<init>(Save.scala:7)
    at com.infosoftcorp.wordpress.ish.InfoSoftHangman.<init>(InfoSoftHangman.scala:35)
    at com.infosoftcorp.wordpress.ish.desktop.DesktopLauncher.main(DesktopLauncher.java:17)
Caused by: java.lang.NullPointerException
    at com.infosoftcorp.wordpress.ish.FileHandler$.<init>(FileHandler.scala:25)
    at com.infosoftcorp.wordpress.ish.FileHandler$.<clinit>(FileHandler.scala)
... 3 more

文件结构:

.
├── android
│   ├── android.iml
│   ├── AndroidManifest.xml
│   ├── assets
│   │   ├── icon.png
│   │   ├── img
│   │   │   ├── 10.png
│   │   │   ├── 11.png
│   │   │   ├── 1.png
│   │   │   ├── 2.png
│   │   │   ├── 3.png
│   │   │   ├── 4.png
│   │   │   ├── 5.png
│   │   │   ├── 6.png
│   │   │   ├── 7.png
│   │   │   ├── 8.png
│   │   │   ├── 9.png
│   │   │   └── line.png
│   │   └── words.yml
│   ├── build.gradle
│   ├── ic_launcher-web.png
│   ├── libs
│   │   ├── armeabi
│   │   │   ├── libgdx-bullet.so
│   │   │   └── libgdx.so
│   │   ├── armeabi-v7a
│   │   │   ├── libgdx-bullet.so
│   │   │   └── libgdx.so
│   │   └── x86
│   │       ├── libgdx-bullet.so
│   │       └── libgdx.so
│   ├── proguard-project.txt
│   ├── project.properties
│   ├── res
│   │   ├── drawable-hdpi
│   │   │   └── ic_launcher.png
│   │   ├── drawable-mdpi
│   │   │   └── ic_launcher.png
│   │   ├── drawable-xhdpi
│   │   │   └── ic_launcher.png
│   │   ├── drawable-xxhdpi
│   │   │   └── ic_launcher.png
│   │   ├── values
│   │   │   ├── strings.xml
│   │   │   └── styles.xml
│   │   └── values-hu
│   │       └── strings.xml
│   └── src
│       └── com
│           └── infosoftcorp
│               └── wordpress
│                   └── ish
│                       └── android
│                           └── AndroidLauncher.java
├── asd.html
├── bitbucket-pipelines.yml
├── build
│   ├── android-profile
│   │   ├── profile-2018-02-12-00-57-01-965.rawproto
│   │   ├── profile-2018-02-12-00-58-00-422.rawproto
│   │   ├── profile-2018-02-12-00-59-18-263.rawproto
│   │   ├── profile-2018-02-12-00-59-55-075.rawproto
│   │   ├── profile-2018-02-12-01-00-22-650.rawproto
│   │   └── profile-2018-02-12-01-03-02-363.rawproto
│   ├── generated
│   │   ├── mockable-android-26.jar
│   │   └── mockable-android-27.jar
│   └── intermediates
│       ├── lint-cache
│       │   ├── api-versions-6-27.0.1.bin
│       │   ├── typos-en.txt-2.bin
│       │   └── typos-hu.txt-2.bin
│       └── proguard-files
│           ├── proguard-android-optimize.txt-2.3.0
│           └── proguard-android.txt-2.3.0
├── build.gradle
├── core
│   ├── build
│   │   ├── classes
│   │   │   └── main
│   │   │       └── com
│   │   │           └── infosoftcorp
│   │   │               └── wordpress
│   │   │                   └── ish
│   │   │                       ├── FileHandler$$anonfun$1.class
│   │   │                       ├── FileHandler.class
│   │   │                       ├── FileHandler$.class
│   │   │                       ├── InfoSoftHangman$$anonfun$create$1.class
│   │   │                       ├── InfoSoftHangman$$anonfun$drawLines$1$1.class
│   │   │                       ├── InfoSoftHangman.class
│   │   │                       ├── InfoSoftHangman$InputHandler.class
│   │   │                       ├── Parsing$$anonfun$parseString$1.class
│   │   │                       ├── Parsing$$anonfun$removeEach$1.class
│   │   │                       ├── Parsing.class
│   │   │                       ├── Parsing$.class
│   │   │                       ├── Parsing$DecLives.class
│   │   │                       ├── Parsing$HMSignal.class
│   │   │                       ├── Parsing$ParseError.class
│   │   │                       ├── Parsing$ParsingResult.class
│   │   │                       ├── Save.class
│   │   │                       └── Save$.class
│   │   ├── libs
│   │   │   └── core-1.0.jar
│   │   └── tmp
│   │       ├── compileScala
│   │       └── jar
│   │           └── MANIFEST.MF
│   ├── build.gradle
│   ├── core.iml
│   └── src
│       └── com
│           └── infosoftcorp
│               └── wordpress
│                   └── ish
│                       ├── FileHandler.scala
│                       ├── InfoSoftHangman.scala
│                       ├── Parsing.scala
│                       └── Save.scala
├── desktop
│   ├── build
│   │   ├── classes
│   │   │   └── main
│   │   │       └── com
│   │   │           └── infosoftcorp
│   │   │               └── wordpress
│   │   │                   └── ish
│   │   │                       └── desktop
│   │   │                           └── DesktopLauncher.class
│   │   ├── dependency-cache
│   │   ├── libs
│   │   │   └── desktop-1.0.jar
│   │   └── tmp
│   │       └── <chaos>
│   ├── build.gradle
│   ├── desktop.iml
│   ├── out
│   │   └── production
│   │       └── classes
│   └── src
│       └── com
│           └── infosoftcorp
│               └── wordpress
│                   └── ish
│                       └── desktop
│                           └── DesktopLauncher.java
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradle.properties
├── gradlew
├── gradlew.bat
├── is-hangman.iml
├── is-hangman.ipr
├── is-hangman.iws
├── local.properties
├── README.md
└── settings.gradle

I added the working directory for the desktop sub-project

【问题讨论】:

  • 哪一行导致错误?
  • println(Gdx.files.local("words.yml").path()),因为我把调试放在那里,如果我把调试部分从 scala.io.Source.fromFile( gdx.files.internal("words.yml").file()) mkString
  • 请edit你的问题表明这一点。
  • 我在代码中添加了注释 //
  • Gdx.files.local("words.yml") 返回null,这意味着它没有找到words.yml 文件。请编辑您的问题以显示项目的目录结构以及包含words.yml 的文件夹。

标签: android scala libgdx


【解决方案1】:

所以我真的很愚蠢,这就是发生的事情。 当新的 Save 被初始化时,它不是在 create() 函数内部调用的,而是在它之前调用的

嗯,有时我会做一些愚蠢的事情。 很抱歉问了你永远无法回答的愚蠢问题,因为我没有发布代码。

【讨论】:

    猜你喜欢
    • 2019-04-14
    • 2014-08-24
    • 1970-01-01
    • 2015-01-20
    • 2013-03-12
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    相关资源
    最近更新 更多