【问题标题】:How to check an xml file exist in Resources android studio如何检查资源android studio中存在的xml文件
【发布时间】:2017-10-02 08:59:59
【问题描述】:

我在 xml 文件夹下的 res 文件夹中有一个 xml 文件。我需要检查这个文件是否存在。我使用getidentifier 来检查存在但我得到了错误。

如图所示。如果 checkExistence=1 (如果文件存在),我有一个名为 checkExistence 的整数。但应用程序不起作用。这可能是什么原因造成的?

【问题讨论】:

  • 最好将代码放在问题中并给出您面临的错误,而不是给出代码的图像。
  • 将该行放在“onCreate”中。上下文可能在外部不可用。
  • 查看 xml 文件夹中是否存在 XML 文件是开发时检查。在开发过程中,您不需要编写查看 XML 文件是否存在的代码。您可以自己检查文件

标签: android xml android-resources file-exists


【解决方案1】:

最后我解决了这个问题。我所做的是:

Context context=this;
int checkExistence;

..

..

..

final String resourcename= String.valueOf(R.xml.xmlfiletocheck);
    checkExistence = context.getResources().getIdentifier(resourcename,"xml",context.getPackageName());

..

..

if(checkExistence!=0) {
//if number is different than 0, it means file exists
..
..
}

【讨论】:

  • 如果 XML 文件不存在,除了不存在之外还会导致您的代码无法编译
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-12
  • 1970-01-01
  • 2011-05-09
  • 1970-01-01
相关资源
最近更新 更多