【问题标题】:How to call fileList() method from Context class?如何从 Context 类调用 fileList() 方法?
【发布时间】:2014-12-19 06:21:51
【问题描述】:

如何使用上下文类中的fileList() 方法?我在这里做错了什么?

Context conData;
    conData = new Context();
    String[] instrumentFileList = new conData().fileList();

感谢任何帮助

【问题讨论】:

    标签: android android-context filelist


    【解决方案1】:

    在文档中:

    fileList() :返回命名私有文件的字符串数组 与此 Context 的应用程序包相关联。

    意味着您应该使用与应用程序关联的上下文来调用fileList(),而不是通过创建Context 类实例来获取与应用程序关联的私有文件列表。

    您可以通过以下方式获取 Activity 中的文件列表:

     String[] instrumentFileList = YourActivityName.this.fileList();
    

    【讨论】:

      【解决方案2】:

      只需将其更改为

       String[] instrumentFileList = new conData().fileList();
      

       String[] instrumentFileList = conData().fileList();
      

      你已经通过这个conData = new Context();创建了实例

      如果Context 是您的非活动类,那么您可以使用上面的代码。但是您只想使用应用程序的Context,那么您可以使用getApplicationContext();

      【讨论】:

      • 很高兴为您提供帮助。如果它是有用的答案,那么你可以接受它或支持它。
      猜你喜欢
      • 2020-09-18
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多