java – Class.getResource(className)给我NullPointerException
发布时间:2020-05-25 11:15:29 所属栏目:Java 来源:互联网
导读:我已经建立了一个独立于平台的库,我想在J2SE和 Android项目中使用它.在这个库中,我有一个Version类,它从清单中加载它的版本细节.在PC上运行良好,在Android上我得到一个NullPointerException,我无法找出原因. 这是我的班级: public class Version { private s
|
我已经建立了一个独立于平台的库,我想在J2SE和 Android项目中使用它.在这个库中,我有一个Version类,它从清单中加载它的版本细节.在PC上运行良好,在Android上我得到一个NullPointerException,我无法找出原因. 这是我的班级: public class Version {
private static int APPCODE = 12354;
private static int MAJOR;
private static int MINOR;
private static char RELEASE;
private static int BUILD;
private static int PROTOCOL;
static {
try {
Class clazz = Version.class;
String className = clazz.getSimpleName() + ".class";
String classPath = clazz.getResource(className).toString(); //NullPointerException
if (classPath.startsWith("jar")) {
String manifestPath = classPath.substring(0,classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF";
Manifest manifest = new Manifest(new URL(manifestPath).openStream());
Attributes attr = manifest.getMainAttributes();
//APPCODE = Integer.parseInt(attr.getValue("APPCODE"));
MAJOR = Integer.parseInt(attr.getValue("MAJOR"));
MINOR = Integer.parseInt(attr.getValue("MINOR"));
RELEASE = attr.getValue("RELEASE").charAt(0);
BUILD = Integer.parseInt(attr.getValue("BUILD"));
PROTOCOL = Integer.parseInt(attr.getValue("PROTOCOL"));
} else {
System.err.println("Couldn't find manifest,reverting to test mode");
MAJOR = 0;
MINOR = 0;
RELEASE = 'n';
BUILD = 0;
//APPCODE = 12354;
}
} catch (IOException e) {
System.err.println("Failed to load manifest file. " + e);
MAJOR = 0;
MINOR = 0;
RELEASE = '0';
BUILD = 0;
//APPCODE = 12354;
PROTOCOL = 0;
} catch (NumberFormatException e) {
System.err.println("Failed to load manifest file. " + e);
MAJOR = 0;
MINOR = 0;
RELEASE = '0';
BUILD = 0;
//APPCODE = 12354;
PROTOCOL = 0;
}
}
public static int getProtocol() {
return PROTOCOL;
}
public static int getAppCode() {
return APPCODE;
}
public static int getBuildNumber() {
return BUILD;
}
public static int getMajor() {
return MAJOR;
}
public static int getMinor() {
return MINOR;
}
public static char getRelease() {
return RELEASE;
}
}
(请原谅System.err.println()行,这些行用于在PC上进行调试). 为什么这在PC上运行良好但在Android上运行不正常? 完整堆栈跟踪: 03-12 22:13:11.687: E/AndroidRuntime(11780): FATAL EXCEPTION: main 03-12 22:13:11.687: E/AndroidRuntime(11780): java.lang.ExceptionInInitializerError 03-12 22:13:11.687: E/AndroidRuntime(11780): at com.logandam.wififileshare.android.MainActivity.onCreate(MainActivity.java:24) 03-12 22:13:11.687: E/AndroidRuntime(11780): at android.app.Activity.performCreate(Activity.java:4465) 03-12 22:13:11.687: E/AndroidRuntime(11780): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052) 03-12 22:13:11.687: E/AndroidRuntime(11780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932) 03-12 22:13:11.687: E/AndroidRuntime(11780): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993) 03-12 22:13:11.687: E/AndroidRuntime(11780): at android.app.ActivityThread.access$600(ActivityThread.java:127) 03-12 22:13:11.687: E/AndroidRuntime(11780): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159) 03-12 22:13:11.687: E/AndroidRuntime(11780): at android.os.Handler.dispatchMessage(Handler.java:99) 03-12 22:13:11.687: E/AndroidRuntime(11780): at android.os.Looper.loop(Looper.java:137) 03-12 22:13:11.687: E/AndroidRuntime(11780): at android.app.ActivityThread.main(ActivityThread.java:4507) 03-12 22:13:11.687: E/AndroidRuntime(11780): at java.lang.reflect.Method.invokeNative(Native Method) 03-12 22:13:11.687: E/AndroidRuntime(11780): at java.lang.reflect.Method.invoke(Method.java:511) 03-12 22:13:11.687: E/AndroidRuntime(11780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 03-12 22:13:11.687: E/AndroidRuntime(11780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 03-12 22:13:11.687: E/AndroidRuntime(11780): at dalvik.system.NativeStart.main(Native Method) 03-12 22:13:11.687: E/AndroidRuntime(11780): Caused by: java.lang.NullPointerException 03-12 22:13:11.687: E/AndroidRuntime(11780): at com.logandam.wififileshare.net.Version.<clinit>(Version.java:30) 03-12 22:13:11.687: E/AndroidRuntime(11780): ... 15 more 更新:PC和Android上的className = Version.class.在PC上使用getName()而不是getSimpleName()中断仍然无法在Android上运行. 解决方法我不是100%肯定,但我认为这种机制在Android下不像在真正的JVM中那样(因为dalvik本质上不是JVM).我认为(我可能在这里错了),在打包的APK中,JAR不再存在,因此您的资源路径可能是错误的.您的JAR的类将转换为dex文件格式和 added to theclasses.dex file.因此,您无法解析类似foo.bar.class的资源,因为它在类路径中不存在.
尝试将版本信息放入文本(或其他资源)文件中,并将其添加到JAR中并读取.应该正确添加资源文件,您应该能够使用该机制读取它. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
