在创建我的类时,我想用 src 文件夹中名为“techs.xml”的 xml 文件中的数据填充一个数组。
public class MainActivity extends Activity {
private static String username;
public ArrayList<Tech> techList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LogInDialog login = new LogInDialog();
login.show(getFragmentManager(), null);
FileInputStream myInputStream;
File file = new File("src/techs.xml");
File file1 = new File("C:\\Users\\Joe\\My Document\\Classes\\CIS 408 - Mobile Appl Dev\\CII\\src\\techs.xml");
System.out.println(file.exists());
System.out.println(file.getAbsolutePath());
System.out.println(file1.exists());
System.out.println(file1.getAbsolutePath());
try {
myInputStream = new FileInputStream(file);
System.out.println("wat");
techList = new ArrayList(TechParser.parse(myInputStream));
} catch (Exception e) {
e.printStackTrace();
}
}
}
相关区域的 LogCat 中的输出是这样的:
04-30 18:29:07.703: I/Process(7846): Sending signal. PID: 7846 SIG: 9
04-30 18:29:10.923: I/System.out(7921): false
04-30 18:29:10.923: I/System.out(7921): /src/techs.xml
04-30 18:29:10.933: I/System.out(7921): false
04-30 18:29:10.933: I/System.out(7921): /C:\Users\Joe\My Document\Classes\CIS 408 - Mobile Appl Dev\CII\src\techs.xml
04-30 18:29:10.933: W/System.err(7921): java.io.FileNotFoundException: /src/techs.xml: open failed: ENOENT (No such file or directory)
04-30 18:29:10.933: W/System.err(7921): at libcore.io.IoBridge.open(IoBridge.java:406)
04-30 18:29:10.933: W/System.err(7921): at java.io.FileInputStream.<init>(FileInputStream.java:78)
04-30 18:29:10.933: W/System.err(7921): at com.example.cii.MainActivity.onCreate(MainActivity.java:35)
04-30 18:29:10.933: W/System.err(7921): at android.app.Activity.performCreate(Activity.java:4492)
04-30 18:29:10.933: W/System.err(7921): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-30 18:29:10.933: W/System.err(7921): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-30 18:29:10.933: W/System.err(7921): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-30 18:29:10.943: W/System.err(7921): at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-30 18:29:10.943: W/System.err(7921): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-30 18:29:10.943: W/System.err(7921): at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 18:29:10.943: W/System.err(7921): at android.os.Looper.loop(Looper.java:137)
04-30 18:29:10.943: W/System.err(7921): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-30 18:29:10.943: W/System.err(7921): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 18:29:10.943: W/System.err(7921): at java.lang.reflect.Method.invoke(Method.java:511)
04-30 18:29:10.943: W/System.err(7921): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-30 18:29:10.943: W/System.err(7921): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-30 18:29:10.943: W/System.err(7921): at dalvik.system.NativeStart.main(Native Method)
04-30 18:29:10.943: W/System.err(7921): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
04-30 18:29:10.943: W/System.err(7921): at libcore.io.Posix.open(Native Method)
04-30 18:29:10.943: W/System.err(7921): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
04-30 18:29:10.943: W/System.err(7921): at libcore.io.IoBridge.open(IoBridge.java:390)
所以这里的问题似乎是该文件不存在,即使它位于该文件夹(/CII/src/techs.xml)中。我使用文件的绝对版本(我不想在最终版本中这样做)来测试它,它也返回 false。任何人都不能给我任何关于问题可能是什么的提示吗?