Reputation: 195
This is probably a noob question.
I have a line of code that works when it's in a Class file that was created by Android Studio and is within the method of the button used, lets call it MainActivity. The line is:
File myNumbersFile = new File((Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + getString(R.string.my_numbers_file))).toString());
But when I move it to another Class file, that I created myself, just to split up the code, and put it in its own method the getString(R.string.my_numbers_file) part stops working.
The calling of the method is good as I can return values that are created before this line.
the logcat output is:
07-08 20:52:48.646 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ java.io.FileNotFoundException: /storage/emulated/0/Download/LottoDownload/my_numbers.txt: open failed: ENOENT (No such file or directory)
07-08 20:52:48.646 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:420)
07-08 20:52:48.646 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:78)
07-08 20:52:48.646 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.io.FileReader.<init>(FileReader.java:42)
07-08 20:52:48.646 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at au.com.acent.ash.basiclottochecker.variousMethods.obtainCurrentNumbersArray(variousMethods.java:74)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at au.com.acent.ash.basiclottochecker.CheckerActivity.populateButton(CheckerActivity.java:42)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View$1.onClick(View.java:3809)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View.performClick(View.java:4421)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View$PerformClick.run(View.java:17903)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:730)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Looper.loop(Looper.java:213)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5225)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at libcore.io.Posix.open(Native Method)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:404)
07-08 20:52:48.656 5359-5359/au.com.acent.ash.basiclottochecker W/System.err﹕ ... 18 more
The error only occurs at runtime, the compiler lets it through.
Please help or point me to another question that answers this. Thanks
Upvotes: 0
Views: 318
Reputation: 83028
getString() is method from Context class. So you need context object while using inside of your class.
Use context.getString(R.string.my_numbers_file)
You can do it via passing context object to the method in which this code block you are using. Like
public void yourMethod(Context context) {
File myNumbersFile = new File((Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + context.getString(R.string.my_numbers_file))).toString());
}
Upvotes: 1
Reputation: 14408
You can access any of the string resources is with a Context
(i.e. an Activity or Service). What I've usually done in this case, is to simply require the caller to pass in the context
.
i.e. you should use
mContext.getString(R.string.my_numbers_file)
instead of
getString(R.string.my_numbers_file)
Note : mContext
is the Context of Activity from where you are calling your class.
Upvotes: 0