Android - 作为输入流的原始资源

发布于 2024-11-05 04:29:30 字数 2073 浏览 4 评论 0原文

所以我尝试与 Android 建立 HTTPS 连接,使用客户端证书进行验证。我已将此证书导入到 BKS 存储中,并插入到我的 res/raw.txt 文件中。

一切都好。

然而,当我开始运行时,我得到一个恒定的 NullPointerException。
对于上下文,我有一个 Connection 类,扩展了 Application,并且基本构造函数应该将证书作为 InputStream 打开并保持就绪状态。当我来建立连接时,就会使用这个。如下(节略):

public class RCPHandshake2 extends Application {

   InputStream in;

   public RCPHandshake2(){
    super.onCreate();
    in = getResources().openRawResource(R.raw.test);        
   }

}

构建运行良好,但是,当我们到达那里(真实版本中的第 36 行)时,堆栈跟踪如下所示:

java.lang.RuntimeException: Unable to start activity   ComponentInfo{com.<company>.t1v2/com.<company>.t1v2.SplashAndText}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
at android.app.ActivityThread.access$2200(ActivityThread.java:119)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
at com.<company>.<component>.RCPHandshake2.<init>(RCPHandshake2.java:36)
at com.<company>.t1v2.SplashAndText.onCreate(SplashAndText.java:43)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
... 11 more

这似乎出乎意料。有没有人遇到过类似的情况?
永恒的爱和感激,以换取比我更有经验的人的任何解决方案/提示/提示/指示!

PS - 基本上,即使我知道原始资源在那里,并且它出现在 R.java 中,但这似乎认为它无法访问它?是这样吗,还是我错过了一些令人眼花缭乱的东西?

So I'm trying to make an HTTPS connection with Android, using a client certificate to validate. I've imported this certificate into a BKS store, and inserted in my res/raw.

All good.

However, when I come to run, I get a constant NullPointerException.
For context, I have a Connection class, extending Application, and the basic constructor should just open the cert as an InputStream and hold it ready. When I come to make the connection, this will be used. As follows (abridged):

public class RCPHandshake2 extends Application {

   InputStream in;

   public RCPHandshake2(){
    super.onCreate();
    in = getResources().openRawResource(R.raw.test);        
   }

}

The build-up to this runs fine, however, when we get to there (line 36 in real version), the stack trace is like so:

java.lang.RuntimeException: Unable to start activity   ComponentInfo{com.<company>.t1v2/com.<company>.t1v2.SplashAndText}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
at android.app.ActivityThread.access$2200(ActivityThread.java:119)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
at com.<company>.<component>.RCPHandshake2.<init>(RCPHandshake2.java:36)
at com.<company>.t1v2.SplashAndText.onCreate(SplashAndText.java:43)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
... 11 more

This seems unexpected. Has anyone come across similar before?
Eternal love and appreciation in exchange for any solutions/hints/tips/pointers from someone a bit more experienced with it that me!!

PS - Basically, even though I know the raw resource is there, and it appears in R.java, this seems to think it's unable to access it? Is this the case, or am I missing something blinding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

⒈起吃苦の倖褔 2024-11-12 04:29:30

看来您无法从构造函数访问资源,因为该对象尚未初始化。稍后当您第一次真正需要时尝试调用它。

It seems you can't access resources from constructor, as the object has not been initilized yet. Try to call it later, when you really need this first time.

美胚控场 2024-11-12 04:29:30

我也有同样的问题。事实证明,我使用了错误的(空)上下文,因此实际上没有可以通过 getResource() 访问的资源 ->空指针异常...

I had the same problem. Turned out that I used a wrong (empty) Context, so there actually were no resources to access via getResource() -> NullPointerException...

迷离° 2024-11-12 04:29:30

要使用原始 InputStream 打开这些资源,请使用资源 ID(即 R.raw.filename)调用 Resources.openRawResource()。

To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文