java - Class.getResource(className) 给我 NullPointerException

标签 java android nullpointerexception cross-platform android-resources

我制作了一个独立于平台的库,我想在 J2SE 和 Android 项目上使用它。在这个库中,我有一个 Version 类,它从 list 中加载其版本详细信息。在 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。使用 getName() 而不是 getSimpleName() 在 PC 上会中断,但在 Android 上仍然不起作用。

最佳答案

你得到 NullPointerException 因为方法调用 clazz.getResource(className) 返回 null 这意味着 指定的资源找不到类名
将您的资源文件放在一个文件夹中并将其设置为资源文件夹。检查this answer on SO获取更多信息。
您也可以查看 this SO answer有关资源文件夹的详细信息。

关于java - Class.getResource(className) 给我 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15371274/

相关文章:

从数组访问方法时出现 java.lang.NullPointerException 错误

android - Android中无法启动Activity组件信息和Java空指针异常

java - 多线程 JApplet

具有异步任务的 Android CursorAdapter 加载不正确,直到滚动

安卓 : How may ways I can get current location programatically

android - 正确的方法覆盖固定大小的自定义 View

java - 什么是NullPointerException,我该如何解决?

java - Java中同步访问资源(Spring MVC)

java - 如何在Java中使x是整数作为条件

java - 运行时异常 : Unabel to instantiate activity ComponentInfo and NullPointerException