0

I'm trying for a while to make android read from a text file that I already have in the R.raw folder.

I have the function void readfile(Context context) that contains the line

InputStream is = context.getResources().openRawResource(R.raw.data);

However when I try to run the app it crashes at that exact line. Can you tell me a workaround? Or another way to read from that file?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1489886
  • 121
  • 1
  • 4
  • 9

1 Answers1

1

There is obviously a problem with the given context. You should post your logcat.

There is 2 ways to solve your problem.

  1. Pass a correct context: see what is wrong with your program and why the given context is not correct.
  2. Extend Application, and save the application context in a static variable: see this post
Community
  • 1
  • 1
AMerle
  • 4,354
  • 1
  • 28
  • 43
  • Of course! I'm sorry. Here's the logcat report after pasting your line of code instead of mine: `W/dalvikvm(279): threadid=1: thread exiting with uncaught exception (group=0x4001d800)` – user1489886 Jul 18 '12 at 18:58
  • And by the way, Context should be lowercased, as getApplicationContext isn't static(according to my compiler) and that's a static reference right there. – user1489886 Jul 18 '12 at 19:17
  • Well this time I get another error, although on debugging the crash method is quite the same: it crashes at the same line after opening the ZygoteInit$MethodAnd file(which hasn't any content). The error is ` E/AndroidRuntime(289): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.localhost/com.localhost.ShapesActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f040000` – user1489886 Jul 19 '12 at 22:56
  • @user1489886 Ok I thought it was possible to get resources without a context, I will edit my post. – AMerle Jul 20 '12 at 07:04