This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

getResources Problem

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some code that reads from a text file in the res/raw folder.  

At the moment this code is in my MainActivity file.  It works fine.

As good coding practice to de-clutter the main activity, I wanted to put it in a separate basic Java class.
I already have a utility class that does other stuff, named WordsData.

The version of the ReadFile method in that WordsData class looks like this:


Essentially the same but with the textViews stripped out and a return type of ArrayList.

My problem is that getResources line - line 6.  Error message "Cannot resolve method getResources".    No matter what I try - and I've spent a while trying different things before posting - I can't get that line 6 code to compile.  

Is there a specific java class file type I should use instead of the basic New > Java Class option in Android Studio?   Or how can I get the method in the WordsData class to recognise getResources?  I thought the Resources were resources for the whole application and would be available right across the project - but clearly not.

Any steer in the right direction (see what I did there?  :-} ) would be really appreciated.


 
Rancher
Posts: 117
7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's not activity class you need to pass context to it and then call getResources() on the context object
 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Adrian

WordsData is not an Activity .
I had previously tried changing the class signature with:

without success.   And also inside the WordsData class:


which caused the automatic generation of a huge constructor.

From previous research I've been trying various ways to pass in the context as you suggest.  But I just can't seem to find the correct code  needed.

Could you give me a bit more detail?  What change I need to make to pass in and access a Context?   What I need to change to the WordsData class signature, if anything.  What I need to change when I call an instance of that class from main activity, if anything.
Currently just plan on using:


 
Adrian Grabowski
Rancher
Posts: 117
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "without success"?

Should be fine, then when you create instance of this class in your activity just pass "this" as context
 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try:



I get error message:   "CLass or Interface expected"
 
Adrian Grabowski
Rancher
Posts: 117
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds like something silly, check your curly braces. If you get stuck post your whole class here.
 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following your suggestion, I've been through all the curly braces to check they match where they should, and also that none are missing.

Here's the code (without the addition of the Context context parameter):
Everything below the ReadFile method has compiled and run several times while writing this project.


As mentioned earlier, changing the signature to include a Context parameter creates the 'Class or Interface expected' error message.

Any suggestions?

 
Adrian Grabowski
Rancher
Posts: 117
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somehow I missed it before as I'm away from my laptop and browsing on the phone.

Suggestion: Add a constructor which takes context as parameter instead of trying to invent some new language feature ;)
 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, got it!   .

I thought i'd tried this, among the many things I tried, but I realise now I was creating a constructor on the Context class, not my WordsData class.

Thanks for your pointer.  It made me revisit and rethink the problem.
 
Adrian Grabowski
Rancher
Posts: 117
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Sorry if my post was bit vague but I was still at work and typing on my phone. Let us know if you get stuck again.
 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Adrian.  I appreciate that.

There's not the slightest doubt that I'll have more issues before this project is done  :-)

But I'm getting there.  Slowly.
 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adrian Grabowski wrote:Great! Sorry if my post was bit vague but I was still at work and typing on my phone. Let us know if you get stuck again.



Adding Context as an argument to the constructor of the WordsData class worked fine and allowed me to get the content of a text file back to my UI activity directly.  This is the relevant parts of the code in WordsData.  Class signature and constructors:


Method that reads a text file and returns a list to caller:



That works fine if I call it directly from the UI MainActivity. However, now that I've inserted an intermediate class  -  a ViewModel  -  I've hit an issue.

I can retrieve the text file content from WordsData in a similar way, but this time I pass the Contect to the ViewModel and then get the ViewModel to pass the same context to WordsData.



The VM then hands it off to WordsData to get a List of the words which it will send back to the MainActivity.  This code in the ViewModel does that:


 This works, but I've just read that it's a very bad idea to pass a context to a ViewModel as it can lead to memory leaks.

The problem is that the file reading method in WordsData won't function unless the context is passed to it.  I know I can revert to my original approach of accessing WordsData directly from the UI Activity.  

Is that the only option I have or is there some way I can still use the ViewModel without leaving open the potential issue of memory leaks?





 
Everybody! Do the Funky Monkey! Like this tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!