简体   繁体   English

Android - 作为 InputStream 的原始资源

[英]Android - raw resources as InputStream

So I'm trying to make an HTTPS connection with Android, using a client certificate to validate.因此,我尝试使用客户端证书进行验证,与 Android 建立 HTTPS 连接。 I've imported this certificate into a BKS store, and inserted in my res/raw.我已将此证书导入 BKS 商店,并插入到我的 res/raw 中。

All good.都好。

However, when I come to run, I get a constant NullPointerException.但是,当我开始跑步时,我得到一个常量 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.对于上下文,我有一个连接 class,扩展应用程序,基本构造函数应该只是将证书作为 InputStream 打开并准备好。 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:对此的构建运行良好,但是,当我们到达那里(实际版本中的第 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

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? PS - 基本上,即使我知道原始资源在那里,并且它出现在 R.java 中,这似乎认为它无法访问它? Is this the case, or am I missing something blinding?是这样吗,还是我错过了一些令人眼花缭乱的东西?

It seems you can't access resources from constructor, as the object has not been initilized yet.您似乎无法从构造函数访问资源,因为 object 尚未初始化。 Try to call it later, when you really need this first time.尝试稍后调用它,当你第一次真正需要它时。

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...原来我使用了错误的(空)上下文,所以实际上没有资源可以通过 getResource() -> NullPointerException 访问...

To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.要使用原始 InputStream 打开这些资源,请使用资源 ID(即 R.raw.filename)调用 Resources.openRawResource()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM