这可能是一个菜鸟问题。
我有一段代码在Android Studio创建的Class文件中并且在所用按钮的方法之内时起作用,可以将其称为MainActivity。该行是:
File myNumbersFile = new File((Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + getString(R.string.my_numbers_file))).toString());
但是,当我将其移动到另一个Class文件时,我创建了我自己,只是为了拆分代码并将其放入自己的方法中,getString(R.string.my_numbers_file)部分停止工作。
该方法的调用很好,因为我可以返回在此行之前创建的值。
logcat的输出是:
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
该错误仅在运行时发生,编译器允许其通过。
请帮助或指向另一个回答该问题的问题。
谢谢
最佳答案
getString()是Context类的方法。因此,在类内部使用时需要上下文对象。
使用context.getString(R.string.my_numbers_file)
您可以通过将上下文对象传递给您正在使用此代码块的方法来实现。
喜欢
public void yourMethod(Context context) {
File myNumbersFile = new File((Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + context.getString(R.string.my_numbers_file))).toString());
}
关于java - 转到新类的方法时,getString(R.string.MyString)不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24630238/