【发布时间】:2017-11-23 03:37:33
【问题描述】:
如何替换正在运行的应用程序中的一些文件(主要活动文件),例如,
/src/main/java/[com.package.name]/home.java /src/main/res/layout/home.xml
与其他文件:
/src/main/assets/home.java /src/main/assets/home.xml
我试过了:
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
if (result.length() == 5) {
String sourcePath1 = "/src/main/assets/activity_home.xml";
File source1 = new File(sourcePath1);
String sourcePath2 = "/src/main/assets/home.java";
File source2 = new File(sourcePath2);
String destinationPath1 = "/src/main/res/layout/activity_home.xml";
File destination1 = new File(destinationPath1);
String destinationPath2 = "/src/main/java/cz/oscio/oscioosp/home.java";
File destination2 = new File(destinationPath2);
try {
FileUtils.copyFile(source1, destination1);
FileUtils.copyFile(source2, destination2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
但它不起作用,我怀疑文件路径错误。如果我尝试:
getAssets().open("activity_home.xml") //Cannot resolve method 'getAssets()'
但这在/src/main/res/layout/ 的情况下无论如何都无济于事......
编辑:只需执行一次 - 它会更改应用程序本身,更新代码。
报告:
java.io.FileNotFoundException: 源 '/src/main/assets/activity_home.xml' 不存在 在 org.apache.commons.io.FileUtils.copyFile(FileUtils.java:636) 在 org.apache.commons.io.FileUtils.copyFile(FileUtils.java:606) 在 cz.oscio.oscioosp.logging.onPostExecute(logging.java:112) 在 cz.oscio.oscioosp.logging.onPostExecute(logging.java:33) 在 android.os.AsyncTask.finish(AsyncTask.java:651) 在 android.os.AsyncTask.-wrap1(AsyncTask.java) 在 android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:148)
在 android.app.ActivityThread.main(ActivityThread.java:5417) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
【问题讨论】:
-
您的路径以“/”还是“./”开头?因为它会被不同地解释
-
我建议我的路径从应用程序开始,所以我只是从应用程序文件夹...(如 C:/Users/Public/MYAPP/app/src/main/assets/home.xml )和因此我使用了 /src/main...
-
我的意思是,如果您的路径以
/开头,它可能(可能)被解释为非相对路径,即从您的主目录开始。您是否尝试过放置绝对路径(C:/Users/Public/MYAPP/app/src/main/assets/home.xml)? -
不,这不是一个解决方案,因为这个应用程序必须在手机上运行,我需要在 apk 文件夹(已安装的应用程序)中启动......我的布局文件夹已经包含同名文件?我想换一个新的...
-
如何从 app-root 文件夹中找到路径?我做错了吗?
标签: java file android-studio replace path