简体   繁体   中英

Android - raw resources as InputStream

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?

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.

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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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