目前我需要在 Android 应用程序中使用本地 http 文件服务器,因此我决定使用 NanoHttpd SimpleWebServer。但我遇到了这样的异常:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.InputStream.read(byte[])' on a null object reference
at fi.iki.elonen.SimpleWebServer.<clinit>(SimpleWebServer.java:83)
崩溃的部分实际上是静态构造函数
static {
mimeTypes();
InputStream stream = SimpleWebServer.class.getResourceAsStream("/LICENSE.txt");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count;
String text;
try {
while ((count = stream.read(buffer)) >= 0) {
bytes.write(buffer, 0, count);
}
text = bytes.toString("UTF-8");
} catch (IOException e) {
text = "unknown";
}
LICENCE = text;
}
显然,库正在尝试加载某些许可证文件但失败。因此,服务器未创建且未启动。
有人知道如何解决这个问题吗?提前致谢!
(我使用 Android Studio 1.5.1 并通过 gradle 依赖项包含库 'org.nanohttpd:nanohttpd-webserver:2.2.0'
)
最佳答案
Seams InputStream 未创建。这可能是因为类加载器找不到 LICENSE.txt 或没有足够的权限。
关于java - NanoHttpd SimpleWebServer 在静态构造函数上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34173220/